Jump to content

[SOLVED] $_FILES always empty when trying to upload


drumhrd

Recommended Posts

Sorry guys I am sure this is simple..but simple minds can't get it right apparently.

 

I am attempting a file upload.  But testing after $_POST['submit'] it is showing $_FILES being empty.

 

/etc/php5/apache2/php.ini showing file_uploads = On

 

 

 

node the print_r($_FILES);

 

showing Array ( )  everytime

 

 

<?php

    if($_POST['submit']) {


$blacklist = array(".php", ".phtml", ".php3", ".php4", ".php5");
print_r($_FILES);

foreach ($blacklist as $item) {
    if(preg_match("/$item\$/i", $_FILES['userfile'])) {
                echo "We do not allow uploading PHP files\n";
                
exit;
}
}

$imageinfo = getimagesize($_FILES['userfile']);
if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png') {
        echo "Sorry, we only accept GIF and JPEG images\n";
        exit;
}





$uploaddir = '/var/www/web-file-server/band_images/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File uploading failed.\n";
}
    
    
    }
    
    else {
    
?>





<form name="submit" action="<?php echo $SCRIPT_NAME ?>" method="POST" enctype="multipart/form-data">
Select the file to upload: <input type="file" name="userfile">
<input type="submit" name="submit" value="upload">
</form>

<?
}
?>

Link to comment
Share on other sites

The code you posted works on a system where uploads are enabled (except that $SCRIPT_NAME will only exist on some old out of date php configurations) and if you were exceeding any of php's upload size limits the symptoms would be different.

 

What does adding the following three lines of code immediately after the first opening <?php tag show after you submit the form?

 

ini_set("display_startup_errors", "1");
ini_set("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

ok so I change my code a bit..

$_FILES['userfile']

 

is supposed to be

 

$_FILES['userfile']['tmp_name']

 

in the getimagesize, preg_match, and move_uploaded_file functions

 

I added the error checking code so now my script is as follows

 


<?php
ini_set("display_startup_errors", "1");
ini_set("display_errors", "1");
error_reporting(E_ALL);

    if($_POST['submit']) {


$blacklist = array(".php", ".phtml", ".php3", ".php4", ".php5");
print_r($_FILES);

foreach ($blacklist as $item) {
    if(preg_match("/$item\$/i", $_FILES['userfile']['tmp_name'])) {
                echo "We do not allow uploading PHP files\n";
                
exit;
}
}

$imageinfo = getimagesize($_FILES['userfile']['tmp_name']);
if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png') {
        echo "Sorry, we only accept GIF and JPEG images\n";
        exit;
}





$uploaddir = '/var/www/web-file-server/band_images/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['tmp_name']);
echo $uploadfile;

if (move_uploaded_file($_FILES['userfile'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "<br />File uploading failed.\n";
}
    
    
    }
    
    else {
    
?>




<form name="submit" action="https://www.artists2you.com/s/upload.php" method="POST" enctype="multipart/form-data">
Select the file to upload: <input type="file" name="userfile">
<input type="submit" name="submit" value="upload">
</form>

<?
}
?>

 

 

now I am getting the following output from the browsers

 

 

Array ( [userfile] => Array ( [name] => bottom-bar.gif [type] => image/gif [tmp_name] => /tmp/phpKJwnWE [error] => 0 => 283 ) ) /var/www/web-file-server/band_images/phpKJwnWE

Notice: Array to string conversion in /var/www/s/upload.php on line 34

 

File uploading failed.

 

 

the move_uploaded_file functions appears to not be working.

 

went through and chmod 777 web-file-server/band_images.

 

phpinfo() showing file_uploads = on

 

I dunno..I am stuck.  please help!!

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.