Jump to content

Problems with very simple upload.php Can anyone help? Sorry, beginner!

- - - - -

  • Please log in to reply
16 replies to this topic

#1
daraclare

daraclare

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Hi folks,

I am trying to do a very simple upload file form and I keep getting this error:

"Sorry, there was a problem uploading your file."

The permission are correct (777) on the destination folder "upload"

Is it something to do with:

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))

Do I need to change something on that line or do I need to create a new directory?

Apologies if these are all very amateur questions but that's what I am!

The code is here, I hope it's okay to put it here:

xxx


Here is the download version:

[ATTACH]3636[/ATTACH]

[ATTACH]3637[/ATTACH]

Thanks in advance,

DC

Attached Files



#2
Spull

Spull

    Newbie

  • Members
  • Pip
  • 1 posts
Can you share upload.php via Pastebin? I downloaded it and it's contents were "Sorry, there was a problem uploading your file."

#3
daraclare

daraclare

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Sorry I didn't realise and I'm not subscribed to this thread so I didn't think anyone answered! Sorry! Here's the code in upload.php

 <?php 

 $target = "upload/"; 

 $target = $target . basename( $_FILES['uploaded']['name']) ; 

 $ok=1; 

 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 

 {

 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";

 } 

 else {

 echo "Sorry, there was a problem uploading your file.";

 }

 ?> 


#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
The first mistake I see, is you had named your form's upload file element "uploadedfile", but reference "uploaded" instead in your PHP. Try changing it to $_FILES['uploadedfile'].
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
daraclare

daraclare

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Hiya,

Firstly, I'm a complete novice, apologies! But I have got my upload.php working which is nice. I changed the code completely so I'll post it again below.

However, I would now like to restrict the file size and file type to only word documents.

I currently have a restriction of 200KB but it's not working - no idea why as I've looked at other similar codes and they look the same.

Also, just to complicate things - can I stop files overwriting each other when uploaded? At the moment, if 2 people upload files with the same name one will overwrite the other.

Is this too many questions in 1?

Any help is very much appreciated!

Code below:

<form enctype="multipart/form-data" action="careers.php" method="POST">

        Please choose a file: <input name="uploaded" type="file" /><br />

<input type="submit" value="Upload" />

</form>


<?php 

 $target = "upload/"; 

 $target = $target . basename( $_FILES['uploaded']['name']) ; 

 $ok=1; 

 

 //This is our size condition 

 if ($uploaded_size > 200) 

 { 

 echo "Your file is too large.<br>"; 

 $ok=0; 

 } 

 

 //This is our limit file type condition 

 if ($uploaded_type =="text/php") 

 { 

 echo "No PHP files<br>"; 

 $ok=0; 

 } 

 

 //Here we check that $ok was not set to 0 by an error 

 if ($ok==0) 

 { 

 Echo "Sorry your file was not uploaded"; 

 } 

 

 //If everything is ok we try to upload it 

 else 

 { 

 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 

 { 

 echo "Your file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded."; 

 } 

 else 

 { 

 echo "Sorry, there was a problem uploading your file."; 

 } 

 } 

 ?>


#6
daraclare

daraclare

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I'm not receiving notification even though i am subscribed and I think my default is instant notification, am i missing something? It makes me look very rude when I don't answer people who are willing to help, sorry folks! I will try and check it hourly

Edited by daraclare, 20 January 2011 - 05:15 AM.


#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
No worries about posting times, we have a relaxed environment here.

To check the size of the file, you need to use a size checking function, such as filesize(), so it would look like this:
 //We check the uploaded file's size, 200000 Bytes = 200KB
 if (filesize($_FILES['uploaded']['tmp_name']) > 200000)  
{  
 echo "Your file is too large.<br>";  
 $ok=0;  
}  
This will directly check the temporary file of which they uploaded to.

You can also change your HTML form as well to check the file before it is uploaded:
<form enctype="multipart/form-data" action="careers.php" method="POST"> 
        Please choose a file: <input name="uploaded" type="file" /><br /> 
<input type="hidden" name="MAX_FILE_SIZE" value="200000" />
<input type="submit" value="Upload" /> 
</form>
  
Next you need to check the MIME type to see if it is a doc format, this may be different depending on the format, so this may work:
 //if MIME is not msword
if ($_FILES['uploaded']['type'] != "application/msword")  
{  
 echo "File is not Microsoft Word document<br>";  
 $ok=0;  
}  
And finally to see if the file already exists, we need to check if the target file exists:

//if location where it is going to exists, detect that.
if(file_exists($target) 
{
  echo "Please select a different name, file already exists.<br/>";
  $ok = 0;
}
I have not tested it, so feel free to integrate those changes and we can fix the problems as they come along!

~Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
daraclare

daraclare

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Hi, thanks for all that it was great! I will officially "Thank You"

The code for "check the size of the file" didn't work in PHP but did when I added the HTML code - Do I need both or can I remove from PHP? The code for file type works perfectly. Nice one! I thought I'd have to find out all the different file types that word does.

The last code to see if the file already exists isn't working for me, I get "Parse error: syntax error, unexpected '{'/public_html/careers.php on line 93"

I will put the code in as it is now - maybe I messed up putting it in but I can't see how:

<?php 

 $target = "upload/"; 

 $target = $target . basename( $_FILES['uploaded']['name']) ; 

 $ok=1; 

 

 //We check the uploaded file's size, 200000 Bytes = 200KB

 if (filesize($_FILES['uploaded']['tmp_name']) > 200000)  

{  

 echo "Your file is too large.<br>";  

 $ok=0;  

} 


//if location where it is going to exists, detect that.

if(file_exists($target) 

{

  echo "Please select a different name, file already exists.<br/>";

  $ok = 0;

}  

 

 //if MIME is not msword

if ($_FILES['uploaded']['type'] != "application/msword")  

{  

 echo "File is not Microsoft Word document<br>";  

 $ok=0;  

}  

 

 //Here we check that $ok was not set to 0 by an error 

 if ($ok==0) 

 { 

 Echo "Sorry your file was not uploaded"; 

 } 

 

 //If everything is ok we try to upload it 

 else 

 { 

 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 

 { 

 echo "Your file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded."; 

 } 

 else 

 { 

 echo "Sorry, there was a problem uploading your file."; 

 } 

 } 

 ?> 


#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I made a few mistakes, it was very late. I had tested this code (and corrected the file size checking in the PHP part)
<?php 
 $target = "upload/"; 
 $target = $target . basename( $_FILES['uploaded']['name']) ; 
 $ok=1; 
 
 //We check the uploaded file's size, 200000 Bytes = 200KB
 if($_FILES['uploaded']['size']) > 200000)  
{  
 echo "Your file is too large.<br>";  
 $ok=0;  
} 

//if location where it is going to exists, detect that.
if(file_exists($target)) 
{
  echo "Please select a different name, file already exists.<br/>";
  $ok = 0;
}  
 
 //if MIME is not msword
if ($_FILES['uploaded']['type'] != "application/msword")  
{  
 echo "File is not Microsoft Word document<br>";  
 $ok=0;  
}  
 
 //Here we check that $ok was not set to 0 by an error 
 if ($ok==0) 
 { 
 Echo "Sorry your file was not uploaded"; 
 } 
 
 //If everything is ok we try to upload it 
 else 
 { 
 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 { 
 echo "Your file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded."; 
 } 
 else 
 { 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 } 
 ?> 

You should always check the size in PHP, the HTML part does it in browser so they won't upload a huge file and then have it checked, which could possibly waste bandwidth.

~Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
daraclare

daraclare

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Thanks, I still get the same error when uploading a file now:

Parse error: syntax error, unexpected '>' in /home/site/public_html/careers.php on line 85

#11
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Ah, you may have missed a semicolon or something, what are the group of lines around line 85?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#12
daraclare

daraclare

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Have I?

Here's the code starting at 84. I can't see anything but that doesn't mean it's not there. I need to learn faster!


 //We check the uploaded file's size, 200000 Bytes = 200KB

 if($_FILES['uploaded']['size']) > 200000)  

{  

 echo "Your file is too large.<br>";  

 $ok=0;  

} 





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users