Jump to content

I can't upload an image


mikemotorcade

Recommended Posts

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

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 :
[code]
$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>";
}[/code]
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

[!--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:

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

and

[code]if (is_uploaded_file($_FILES['imagefile']['tmp_name'])) {  continue with code... }[/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.


Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[!--quoteo(post=374609:date=May 17 2006, 02:31 AM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ May 17 2006, 02:31 AM) [snapback]374609[/snapback][/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).
Link to comment
Share on other sites

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



html form:

[code]<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>[/code]


upload.php:





[code]




$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;
}




[/code]








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
Link to comment
Share on other sites

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:

[code]
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";
[/code]
You were using 'file' as the associative array index instead of 'image' (to match your html form).
Link to comment
Share on other sites

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

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

to

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

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.