Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-22-2011, 09:14 PM   PM User | #1
ditchfieldcaleb
New Coder

 
Join Date: Dec 2010
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
ditchfieldcaleb is an unknown quantity at this point
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.
ditchfieldcaleb is offline   Reply With Quote
Old 02-22-2011, 09:42 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You're not checking the return value of your call to move_uploaded_file(). You need to make sure that call worked.
__________________
Fumigator is offline   Reply With Quote
Old 02-22-2011, 09:54 PM   PM User | #3
ditchfieldcaleb
New Coder

 
Join Date: Dec 2010
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
ditchfieldcaleb is an unknown quantity at this point
Quote:
Originally Posted by Fumigator View Post
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.
ditchfieldcaleb is offline   Reply With Quote
Old 02-22-2011, 10:16 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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";

__________________
Fumigator is offline   Reply With Quote
Old 02-22-2011, 11:14 PM   PM User | #5
ditchfieldcaleb
New Coder

 
Join Date: Dec 2010
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
ditchfieldcaleb is an unknown quantity at this point
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.
ditchfieldcaleb is offline   Reply With Quote
Old 02-22-2011, 11:46 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-22-2011, 11:54 PM   PM User | #7
ditchfieldcaleb
New Coder

 
Join Date: Dec 2010
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
ditchfieldcaleb is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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?
ditchfieldcaleb is offline   Reply With Quote
Old 02-23-2011, 01:47 AM   PM User | #8
fommerjackson
New to the CF scene

 
Join Date: Feb 2011
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
fommerjackson is an unknown quantity at this point
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
fommerjackson is offline   Reply With Quote
Users who have thanked fommerjackson for this post:
ditchfieldcaleb (02-23-2011)
Old 02-23-2011, 02:17 AM   PM User | #9
ditchfieldcaleb
New Coder

 
Join Date: Dec 2010
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
ditchfieldcaleb is an unknown quantity at this point
Quote:
Originally Posted by fommerjackson View Post
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?
ditchfieldcaleb is offline   Reply With Quote
Old 02-23-2011, 02:47 AM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
ditchfieldcaleb (02-23-2011)
Old 02-23-2011, 01:18 PM   PM User | #11
ditchfieldcaleb
New Coder

 
Join Date: Dec 2010
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
ditchfieldcaleb is an unknown quantity at this point
That worked! It is a permissions problem. I'll have my friend help me with the rest. Thanks!
ditchfieldcaleb is offline   Reply With Quote
Reply

Bookmarks

Tags
file, form, php, upload

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:42 AM.


Advertisement
Log in to turn off these ads.