• 0

Can't get a php upload script to work, errrr


Question

I'm trying to get a very basic PHP upload script to work.

 

This is what I have in my HTML page:

<form enctype="multipart/form-data" action="/ftp/upload.php" method="POST">
    Select your file to upload:<br><br> <input name="uploaded" type="file" />
    <br><br><br><input type="submit" value="Upload" />
</form>

This is my upload.php file:

<?php 
 $target = "/ftp/Ducati Tuning/Uploads/"; 
 $target = $target . basename( $_FILES['uploads']['name']) ; 
 $ok=1; 
 
 //This is our size condition 
 if ($uploaded_size > 350000) 
 { 
 echo "Your file is too large.<br>"; 
 $ok=0; 
 } 
 
 //This is our limit file type condition 
 if ($uploaded_type =="bin/xdf/txt") 
 { 
 echo "You may only upload .BIN, .XDF, & .TXT 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['uploads']['tmp_name'], $target)) 
 { 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
 } 
 else 
 { 
 echo "Sorry, there was a problem uploading your file.; 
 } 
 } 
 ?>

Can anyone spot my mistake? It's driving me nuts lol. I have the Uploads directory which is my file dstination folder set for 777 on the permissions already. Perhaps I need to do something with a .htaccess file on my server too? I'm using GoDaddy. In my PHP config File Uploads is turned on and the session /tmp is also set to 777.

 

Any help is greatly appreciated in advance! Thanks!

 

Alex

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

[04-Sep-2014 16:14:28] PHP Notice:  Undefined index:  uploads in /ftp/upload.php on line 3
[04-Sep-2014 16:14:28] PHP Notice:  Undefined variable: uploaded_size in /ftp/upload.php on line 7
[04-Sep-2014 16:14:28] PHP Notice:  Undefined variable: uploaded_type in /ftp/upload.php on line 14
[04-Sep-2014 16:14:28] PHP Notice:  Undefined index:  uploads in /ftp/upload.php on line 29
 

I just removed the file size and type variables from my .php file to remove those errors just for now but I still have the index errors...

Link to comment
Share on other sites

  • 0

Those aren't errors, they are notices your script should theoretically still work with those notices.

 

However..

 

$_FILES['uploads']['name'] is grabbing the field 'uploads' but your file field is called uploaded.

 

I doubt this: if ($uploaded_type =="bin/xdf/txt")  ... will work

 

line 23, remove the capital E from Echo, it should be echo.

 

and on line 35 you are missing the closing quote.

 

..edit, infact, the whole thing is a mess.

  • Like 1
Link to comment
Share on other sites

  • 0

Ok, I updated the field to be called uploads as well.

 

I changed the E to e.

 

I'm not sure exactly where I should put the additional quote you said I'm missing?

 

I got it from php.about.com so it could have be writtin a mess in their Wiki. i'm just trying to work with it.

 

Now my errors are:

 

[04-Sep-2014 16:42:18] PHP Notice:  Undefined variable: uploaded_size in /ftp/upload.php on line 7
[04-Sep-2014 16:42:18] PHP Notice:  Undefined variable: uploaded_type in /ftp/upload.php on line 14
[04-Sep-2014 16:42:18] PHP Warning:  move_uploaded_file(/ftp/Ducati Tuning/Uploads/2009 Ducati 1198 Base Model - Stock.bin) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: No such file or directory in /ftp/upload.php on line 29
[04-Sep-2014 16:42:18] PHP Warning:  move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move '/tmp/phpAKmNAL' to '/ftp/Ducati Tuning/Uploads/2009 Ducati 1198 Base Model - Stock.bin' in /ftp/upload.php on line 29
 

I'm not sure exactly where in line 29 I should put the directory?

Link to comment
Share on other sites

  • 0

The problem has nothing to do with directory permissions or .htaccess. The problem is your PHP code, and the PHP errors you've posted describe exactly what you're doing wrong. These are pretty simple mistakes to make, and the fact that you're not understanding the error messages suggests that you must be extremely new to this.

1) You've called your file input field in your form 'uploaded', yet you're trying to access it for some reason as $_FILES['uploads'] and even at one point $_FILES['uploadedfile'] instead of $_FILES['uploaded'].

2) On lines 7 and 14 you are trying to compare the contents of a variable (uploaded_size and uploaded_type respectively) with something, yet you haven't yet created (defined) and initialised these variables yet. To be clear, as far as the if statements on lines 7 and 14 are concerned, variables uploaded_size and uploaded_type do not exist, so why do you think they do and that they might contain these values you're comparing their values to?!

Link to comment
Share on other sites

  • 0

So I need to rename $_FILES['uploadedfile'] to $_FILES['uploads'] ?

 

I'll Google for how to define and initiate those other fields then for size and type.

 

This is my first time very doing anything in php.

 

Thanks for the help!

Link to comment
Share on other sites

  • 0

This should work but untested, it should give you a better idea how to refactor your code. your mistakes are pretty basic stuff so hopefully you can see the differences between them
 

<form enctype="multipart/form-data" action="/ftp/upload.php" method="POST">
    <input type="file" name="uploaded_file" id="uploaded_file">
    <input type="submit" value="Upload!">
</form>


<?php 
$target = "/ftp/Ducati Tuning/Uploads/"; 
$ok = 1; 

// This is our size condition 
if($_FILES['uploaded_file']['size'] > 350000)
{
  echo "Your file is too large."; 
  $ok = 0; 
}

// Allowed file types
$mimes = array('application/octet-stream', 'application/xcap-diff+xml', 'text/plain');

if(!in_array($_FILES['uploaded_file']['type'], $mimes)) && (!empty($_FILES["uploaded_file"]["type"]))
{
  echo "You may only upload .BIN, .XDF, & .TXT files!"; 
  $ok = 0; 
}

if($ok == 0) 
{ 
  echo "Sorry your file was not uploaded"; 
} 
else 
{ 
  if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target . $_FILES['uploaded_file']['name'])) 
  { 
    echo "The file ". basename($_FILES['uploaded_file']['name']). " has been uploaded."; 
  } 
  else 
  { 
    echo "Sorry, there was a problem uploading your file."; 
  } 
} 
?>
Link to comment
Share on other sites

  • 0

 

 

I'm not sure exactly where I should put the additional quote you said I'm missing?

 

 

 

 

Look at the line of code i said:

 

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

 

 

The syntax is:

echo " foo " ;

you done:

echo " foo ;
Link to comment
Share on other sites

  • 0

Ok, now I get when using your example:

 

[04-Sep-2014 17:06:48] PHP Parse error:  syntax error, unexpected T_BOOLEAN_AND in /ftp/upload.php on line 15
 

Allott closer lol. Thanks! So I just removed the line about the file types until I can figure out what the boolean error is and now I get these errors:

 

[04-Sep-2014 17:24:41] PHP Warning:  move_uploaded_file(/ftp/Ducati Tuning/Uploads/2009 Ducati 1198 Base Model - Stock.bin) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: No such file or directory in ftp/upload.php on line 18
[04-Sep-2014 17:24:41] PHP Warning:  move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move '/tmp/phphgwusM' to '/ftp/Ducati Tuning/Uploads/2009 Ducati 1198 Base Model - Stock.bin' in /ftp/upload.php on line 18
 

So I have something wrong in this line, this 18 now:

if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target . $_FILES['uploaded_file']['name']))
Link to comment
Share on other sites

  • 0

Yeah for sure, the upload.php file is in the /ftp directory so that's why I have /ftp/Ducati Tuning/Uploads/ as the path.

 

Perhaps it's a GoDaddy thing? Since the /tmp directory is outside of my /public_html/ which is where I have everything? For example the full path is /public_html//ftp/Ducati Tuning/Uploads/ but the /tmp folder is in the very root of my account. Grrrrr, so close....

 

Even if I add /public_html/ into the front of the path, I still get the error but I know it's right. Gotta be something going on with the way GoDaddy is doing something. I even set the 777 permission thru GoDaddys CP rather than my FTP client.

 

EDIT: I got it! Need to add /home/xxxxxx in front of /public_html//ftp/Ducati Tuning/Uploads/

 

So now if I can figure out that boolean error so I can add the stuff to restrict the file types I'll be stylin!!!!

Link to comment
Share on other sites

  • 0

So I need to rename $_FILES['uploadedfile'] to $_FILES['uploads'] ?

 

I'll Google for how to define and initiate those other fields then for size and type.

 

This is my first time very doing anything in php.

 

Thanks for the help!

You just need to create a variable and stick something in it, like so:

$myNumber = 2;

So for $uploaded_size, for which the value you actually want for this is provided within $_FILES['uploaded_file']['size], you want:

$uploaded_size = $_FILES['uploaded_file']['size];

That is if you want to copy it to a new variable, instead of just using it directly from $_FILES.

The second one is harder to put right; you actually need an array of file types and you need to use the in_array function, as shown in the example code posted above.

 

Ok, now I get when using your example:

 

[04-Sep-2014 17:06:48] PHP Parse error:  syntax error, unexpected T_BOOLEAN_AND in /ftp/upload.php on line 15

 

Allott closer lol. Thanks! So I just removed the line about the file types until I can figure out what the boolean error is and now I get these errors:

 

[04-Sep-2014 17:24:41] PHP Warning:  move_uploaded_file(/ftp/Ducati Tuning/Uploads/2009 Ducati 1198 Base Model - Stock.bin) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: No such file or directory in ftp/upload.php on line 18

[04-Sep-2014 17:24:41] PHP Warning:  move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move '/tmp/phphgwusM' to '/ftp/Ducati Tuning/Uploads/2009 Ducati 1198 Base Model - Stock.bin' in /ftp/upload.php on line 18

 

So I have something wrong in this line, this 18 now:

if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target . $_FILES['uploaded_file']['name']))

Here's a slightly altered version of goodbytes' code, try this:

<?php
$fileUploadDir = "/ftp/Ducati Tuning/Uploads/";

if (!isset($_POST['submit'])) {
?>
<form enctype="multipart/form-data" action="/ftp/upload.php" method="POST">
    <input type="file" name="uploaded_file" id="uploaded_file">
    <input type="submit" value="Upload!" name="submit">
</form>
<?php
} else {

    if ($_FILES['uploaded_file']['error'] > 0) {
        echo "Error: " . $_FILES['uploaded_file']['error'] . "<br>";
        exit();
    }
        
    // This is our size condition
    if ($_FILES['uploaded_file']['size'] > 350000)
    {
        echo "Error: Your file is too large.";
        exit();
    }

    // Allowed file types
    $mimes = array('application/octet-stream', 'application/xcap-diff+xml', 'text/plain');

    if (!in_array($_FILES['uploaded_file']['type'], $mimes))
    {
        echo "Error: Unacceptible file type! You may only upload executable, XDF, and plain-text files!";
        exit();
    }

    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $fileUploadDir . $_FILES['uploaded_file']['name']))
    {
        echo "The file ". basename($_FILES['uploaded_file']['name']). " has been uploaded.";
    } else {
        echo "Error: Sorry, there was a problem uploading your file.";
    }
}
?>

Edit: fixed extra spaces added by damn forum softwarerofl.gif

Link to comment
Share on other sites

  • 0

P.s. you should perhaps review what types of files you're allowing through there. Executable files are usually not allowed. Also, your error message about file type was previously stating .bin, .xdt and .txt only, but .txt for exampel is only one of many file extensions for files of type txt/plain!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.