简体   繁体   中英

PHP Image not uploaded to website

On my website I have 2 separate places for users to upload an image. The first works perfectly. The second doesn't. As far as I can see, they are exactly the same. To test, I used the same image on both, with it working on the first, but not the second. Here is the code from the non-working one:

<?php
include('cn.php');

$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];
$size = $_FILES['image']['size'];
$temp = $_FILES['image']['tmp_name'];
$error = $_FILES['image']['error'];

echo $name; //Doesnt echo anything

$rand_num = rand(1, 1000000000);

$name = $rand_num . "_" . $name;

echo $name; //Echos just the random number followed by _

if ($error > 0) { 
    die("Error uploading file! Go back to the <a href='gallery.php'>gallery</a> page and try again.");
} else {
    $sql = "INSERT INTO gallery (image) VALUES ('".$name."')"; //Inserts the random number to the database
    $query = mysql_query($sql) or die(mysql_error());
    echo $sql; 

    move_uploaded_file($temp, "gallery_pics/$name"); //Doesn't move the file.  gallery_pics exists, if that matters
    echo "Upload complete! Go back to the <a href='gallery.php'>album</a>.";
}

?>

If someone could please help me out here. I am sure it is something simple, but I have yet to find anything that helped. Thanks!

EDIT: There are two lines above the code that won't show up properly. One starts the php and the other opens the database.

Do both scripts reside in the same directory? If not, make sure you specify proper relative or a full path, preceded by slash like this:

move_uploaded_file($temp, "/some_folder/gallery_pics/$name");

Also, do you check the extensions of uploaded files? If not, the user could upload and execute a PHP file, for example. And by the way, your file name would look better this way:

$rand_num = rand(1111111111, 9999999999);

Enable error reporting when developing or trying to find a fault also check your server error logs. Check for the $_FILES['image']['error'] error code and display the error accordingly. Have a go with the code below. hope it helps

<?php
//Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors',1);

$error_types = array(
    0=>"There is no error, the file uploaded with success",
    1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
    2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
    3=>"The uploaded file was only partially uploaded",
    4=>"No file was uploaded",
    6=>"Missing a temporary folder"
);

if($_FILES['image']['error']==0) {
    // process
    $name = $_FILES['image']['name'];
    $type = $_FILES['image']['type'];
    $size = $_FILES['image']['size'];
    $temp = $_FILES['image']['tmp_name'];

    //mt_rand() = 0-RAND_MAX and make filename safe.
    $name = mt_rand()."_".preg_replace('/[^a-zA-Z0-9.-]/s', '_', $name);

    //insert into db, swich to PDO
    ...

    //Move file upload
    if (move_uploaded_file($_FILES['image']['tmp_name'], "gallery_pics/$name")) {
        echo "Upload complete! Go back to the <a href='gallery.php'>album</a>.";
    } else {
        echo "Failed to move uploaded file, check server logs!\n";
    }
} else {
    // error
    $error_message = $error_types[$_FILES['userfile']['error']];
    die("Error uploading file! ($error_message)<br/>Go back to the <a href='gallery.php'>gallery</a> page and try again.");
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM