Jump to content


Photo

I can't upload an image


  • Please log in to reply
9 replies to this topic

#1 mikemotorcade

mikemotorcade
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 May 2006 - 05:37 AM

I need help. Why doesn't this work?


copy($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile']['name']) or die ("Could not copy");

I'm trying to upload images and this line NEVER works.

#2 Alicia

Alicia
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 17 May 2006 - 05:55 AM

[!--quoteo(post=374560:date=May 17 2006, 12:37 AM:name=mikemotorcade)--][div class=\'quotetop\']QUOTE(mikemotorcade @ May 17 2006, 12:37 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I need help. Why doesn't this work?
copy($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile']['name']) or die ("Could not copy");

I'm trying to upload images and this line NEVER works.
[/quote]

have u tried to assign a variable for "files/".$_FILES['imagefile']['name'] ?

something like this :
$file_name = $_FILES['uploadFile']['name'];
// strip file_name of slashes
$copy = copy($_FILES['uploadFile']['tmp_name'],$file_name);
// check if successfully copied
if($copy){
echo "$file_name | uploaded sucessfully!<br>";
}else{
echo "$file_name | could not be uploaded!<br>";
}


#3 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 17 May 2006 - 06:07 AM

Check $_FILES['imagefile']['error'] to see if there's a problem first. You could check and use is_uploaded_file() before using move_uploaded_file() (why copy?).

[a href=\"http://us2.php.net/manual/en/function.is-uploaded-file.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.is-uploaded-file.php[/a]
[a href=\"http://us2.php.net/manual/en/function.move-uploaded-file.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.move-uploaded-file.php[/a]

See this topic: [a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=93319\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=93319[/a]

Double check your HTML form is correct: [a href=\"http://us2.php.net/manual/en/features.file-upload.php#AEN7078\" target=\"_blank\"]http://us2.php.net/manual/en/features.file...oad.php#AEN7078[/a]

FYI:

You have to be very careful when allowing uploading of files for security reasons. You cannot rely on the extension as what the file data actually is. The $_FILES['userfile']['type'] is not reliable because it's sent by the browser (if one is even used) and it determines the MIME type by the file's extension. The file could really be an executable (i.e. .exe) but it's extension changed to .jpg on purpose, and the browser will incorrectly send image/jpeg as the MIME type.

After the file is uploaded to the temporary directory and before moving it to a permanent location find out exactly what type of file it is (and that you allow it for your needs). For picture files, you can use getimagesize() function which returns an array of info including the file type or false if it's not a picture file. See:

[a href=\"http://us2.php.net/manual/en/function.getimagesize.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.getimagesize.php[/a]

For other types of files, you can use finfo_file() but it requires PECL extension (PEAR installation). See:

[a href=\"http://us2.php.net/manual/en/function.finfo-file.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.finfo-file.php[/a]

Or mime_content_type() is available in PHP 4.3.0+:

[a href=\"http://us2.php.net/manual/en/function.mime-content-type.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.mime-content-type.php[/a]


When a file is not acceptable to you, remember to delete it from the temporary directory and give an error to the user (or ban them if they try it too many times).



#4 mikemotorcade

mikemotorcade
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 17 May 2006 - 06:46 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Check $_FILES['imagefile']['error'] to see if there's a problem first. You could check and use is_uploaded_file() before using move_uploaded_file() (why copy?).[/quote]

Same thing happens with move_uploaded_file(). copy just happened to be what I was trying to use when I gave up.

And I checked. there aren't any errors. My old code I was using contained this:

if ($_FILES['imagefile']['error'] > 0) {  continue with code... }

and

if (is_uploaded_file($_FILES['imagefile']['tmp_name'])) {  continue with code... }



[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]have u tried to assign a variable for "files/".$_FILES['imagefile']['name'] ?[/quote]

I just did that. It didn't work.




[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]You have to be very careful when allowing uploading of files for security reasons.[/quote]


Yeah, I know. I had a lot more code than just that line. But I determined that it was the phase where I copy the image to a new location that it fails, so I just posted that line.




#5 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 17 May 2006 - 07:11 AM

Post the actual HTML form and exact relevant code.

Since you're not specifying a forward slash before "files/" it expects that directory to be where the script is running from.

Does the directory exist?
Have you set the correct directory permissions?
Did you read my other post of the link provided?
Have you checked php.ini settings?
Have you double checked the form?
Do you have the MAX_FILE_SIZE set in the HTML?
How big is the file you're trying to upload?
What platform are you using (Linux/Windows)?
What server are you using?
What version of PHP are you using?
Where does it stop working, copy, so does it return false then?
Have you put displays to debug this and follow the logic flow?
Do you have error_reporting set and display_errors on?

You got to give us more to work with here.

#6 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 17 May 2006 - 09:31 AM

i must appreciate, toplay that was one complete check list.

can we add a suggestion in that to use move_uploaded_file() instead of copy()....

some how i dont prefer copy() in case of uploaded files....

just a thought.
Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#7 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 17 May 2006 - 02:54 PM

[!--quoteo(post=374609:date=May 17 2006, 02:31 AM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ May 17 2006, 02:31 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
i must appreciate, toplay that was one complete check list.

can we add a suggestion in that to use move_uploaded_file() instead of copy()....

some how i dont prefer copy() in case of uploaded files....

just a thought.
[/quote]
LOL. Thanks. I already mentioned about the move in my first post here.

Members are always encouraged to post details about their problem so members on this forum can help them easier. Not posting all code or massaged (not exact) code, or not the HTML when relevant, or not the exact error(s) messages leads to confusion and long topics before their problems are solved (if at all).

#8 mikemotorcade

mikemotorcade
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 18 May 2006 - 04:22 AM

Ok, so here is the latest version I have that isn't working:



html form:

<FORM ENCTYPE="multipart/form-data" ACTION="upload.php" METHOD="post" name="upload" id="upload">

<INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="2000000">


<B>Image:</B><BR>
<INPUT TYPE="file" id="image" name="image" VALUE="" SIZE="60" MAXLENGTH="100"><BR>
<BR>
<BR>

<INPUT TYPE="SUBMIT" VALUE="Upload Image">
</FORM>


upload.php:









$error = '';

//test for error
if ($_FILES['image']['error'] > 0) {
  switch ($_FILES['image']['error']) {
    case 1: $error = 'Error[1]: Size exeeded maximum file size allowed.<BR><BR><BR>';
    case 2: $error = 'Error[2]: Size exeeded maximum file size allowed.<BR><BR><BR>';
    case 3: $error = 'Error[3]: File only partially uploaded<BR><BR><BR>';
    case 4: $error = 'Error[4]: No file uploaded<BR><BR><BR>';
  }
}

if ($error == '') {
  $upfile = '/images/'.$_FILES['image']['name'];
  if (is_uploaded_file($_FILES['image']['tmp_name'])) {
    if (!move_uploaded_file($_FILES['image']['tmp_name'], $upfile)) {
       $error = 'Error: Couldn\'t move file to destination directory<BR><BR><BR>';
    }
  }else{
    $error = 'Error: Image not uploaded';
  }
} 
 
if ($error == '') {

  echo '<IMG SRC="'.$DOCUMENT_ROOT.'/images/'.$_FILES['file']['name'].'"><BR>';
  echo "<B>FILE UPLOADED:</B><BR><BR><HR><I>\"$_FILES['file']['name']\" successfully uploaded<BR></I><HR><BR><BR>\n";
  echo '<A HREF="eventimages.php">Add Another Image</A><BR>';
 
 
}else{
  echo $error;
}












Does the directory exist?
yes

Have you set the correct directory permissions?
I chmodded the images dir 777

Did you read my other post of the link provided?
yeah, see below

Have you checked php.ini settings?
Ummm, How do I go about doing that? I was thinking maybe I might have allow upload set to false. I don't know I didn't setup php. I'm using cPanel if that means anything.. it already has everything installed on it, and I don't even know how to access the php.ini file. I don't even know if I can. ???

What server are you using?
linux

What version of PHP are you using?
4.4.1

#9 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 18 May 2006 - 02:24 PM

You need to check to make sure that file_uploads option is on in the php.ini file. See:
[a href=\"http://us2.php.net/manual/en/ini.core.php#ini.file-uploads\" target=\"_blank\"]http://us2.php.net/manual/en/ini.core.php#ini.file-uploads[/a]

You can view your php.ini file settings by using:
[a href=\"http://us2.php.net/manual/en/function.phpinfo.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.phpinfo.php[/a]

Apparently you're not seeing the PHP syntax error on one of the echo lines. While testing/debugging, make sure you have error_reporting(E_ALL); at the top of your script so you can see all of PHP's errors/warnings/notices. Also, have display_errors on in the php.ini file. Otherwise, set it using ini_set('display_errors', '1'); at the top of your script too.

The two echo statements towards the end of your script need to be changed to the following:

echo '<IMG SRC="'.$_SERVER['DOCUMENT_ROOT'].'/images/'.$_FILES['image']['name'].'"><BR>';
echo "<B>FILE UPLOADED:</B><BR><BR><HR><I>\"{$_FILES['image']['name']}\" successfully uploaded<BR></I><HR><BR><BR>\n";
You were using 'file' as the associative array index instead of 'image' (to match your html form).


#10 mikemotorcade

mikemotorcade
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 19 May 2006 - 08:13 PM

Thank you very much everything works now. I had uploads set to on. But i was able to see the error message thanks to you explaining how to do that and it said that I was trying to upload the file to "" (an empty string.) ???

I ended up changing the path variable from

$upfile = '/images/'.$_FILES['image']['name'];

to

$upfile = $_SERVER['DOCUMENT_ROOT'].'/images/'.$_FILES['image']['name'];

and it worked.

So I don't really know exactly what my problem was, but it works now. Thanks for all your help. I really appreciate it.


-Michael




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users