CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   PHP File Upload not working (http://www.codingforums.com/showthread.php?t=219442)

ditchfieldcaleb 02-22-2011 09:14 PM

PHP File Upload not working
 
Ok, so I have a form as follows:
Code:

<form action="submit.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

The page that 'receives' the file has the following code that handles the uploaded file:
PHP Code:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| (
$_FILES["file"]["type"] == "image/jpeg")
|| (
$_FILES["file"]["type"] == "image/pjpeg"))
&& (
$_FILES["file"]["size"] < 200000000))
  {
  if (
$_FILES["file"]["error"] > 0)
    {
    echo 
"Return Code: " $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo 
"Upload: " $_FILES["file"]["name"] . "<br />";
    echo 
"Type: " $_FILES["file"]["type"] . "<br />";
    echo 
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo 
"Temp file: " $_FILES["file"]["tmp_name"] . "<br />";

    if (
file_exists("uploads/" $_FILES["file"]["name"]))
      {
      echo 
$_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      
move_uploaded_file($_FILES["file"]["tmp_name"],
      
"uploads/" $_FILES["file"]["name"]);
      echo 
"Stored in: " "uploads/" $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo 
"Invalid file";
  }
?>

So the code says in the browser that it works, but the file itself is not actually showing up on my server. You can try out the code here: northside.mcsdga.net/reveille/staffsubmit.php I've checked the folder name, and it is correct.

Fumigator 02-22-2011 09:42 PM

You're not checking the return value of your call to move_uploaded_file(). You need to make sure that call worked.

ditchfieldcaleb 02-22-2011 09:54 PM

Quote:

Originally Posted by Fumigator (Post 1057108)
You're not checking the return value of your call to move_uploaded_file(). You need to make sure that call worked.

How would I do that? Sorry, I'm quite new to PHP.

Fumigator 02-22-2011 10:16 PM

The function returns TRUE or FALSE; you can assign it to a variable and check it:

PHP Code:

$moveResult move_uploaded_file();
if (!
$moveResult)
{
    echo 
"ERROR";


Or just shortcut and check it directly:

PHP Code:

if (!move_uploaded_file())
{
    echo 
"ERROR";



ditchfieldcaleb 02-22-2011 11:14 PM

Ok, so I implemented the code to check my move, and I keep getting the error code showing that it's not successfully moving it. Any idea why? Ask me for specs if you need them.

Fou-Lu 02-22-2011 11:46 PM

Chances are its a permission issue.
Replace the "Error" with a print_r(error_get_last());. One of the offsets will be message, and I'm betting its either going to be a non-existent directory, or a privilege problem (permission denied).

ditchfieldcaleb 02-22-2011 11:54 PM

Quote:

Originally Posted by Fou-Lu (Post 1057158)
Chances are its a permission issue.
Replace the "Error" with a print_r(error_get_last());. One of the offsets will be message, and I'm betting its either going to be a non-existent directory, or a privilege problem (permission denied).

Ok, so I replace the "Error" with the code . . . nothing shows up in the browser. What should I do if it is a permissions problem?

fommerjackson 02-23-2011 01:47 AM

most likely this is a file permissions problem.

make sure your upload property is set to 0777

also using your ftp client, chmod your directory to 0777 as well

ditchfieldcaleb 02-23-2011 02:17 AM

Quote:

Originally Posted by fommerjackson (Post 1057193)
most likely this is a file permissions problem.

make sure your upload property is set to 0777

also using your ftp client, chmod your directory to 0777 as well

Umm . . . I use Adobe Dreamweaver CS5 to upload my files . . . can you explain to me how to do these things?

Fou-Lu 02-23-2011 02:47 AM

The absolute easiest thing you can do is use mkdir from PHP. That automatically assigns proper ownership. Otherwise, you may need to login through your webhost and chmod the directory manually using SSH.

If you are getting a white page, that indicates a fatal error, assuming that there is supposed to be some kind of output. Enable your error reporting by putting this at the top of the page:
PHP Code:

ini_set('display_errors'1);
error_reporting(E_ALL); 

If the only thing that has changed was the addition of error_get_last, then the error will be a non-existent function. Error_get_last is only post 5.2. To get around that, you would instead track errors with:
PHP Code:

ini_set('track_errors'1); // Somewhere usually at the top

...
echo 
$php_errormsg// After your attempt to move the uploaded file 

Chances are your error reporting is too low. As soon as you open it up to E_ALL, then you will likely see the error without needing to use error_get_last or $php_errormsg.

ditchfieldcaleb 02-23-2011 01:18 PM

That worked! It is a permissions problem. I'll have my friend help me with the rest. Thanks!


All times are GMT +1. The time now is 06:32 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.