donglizuo8892 2012-11-01 13:46
浏览 18

PHP图片未上传到网站

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.

  • 写回答

2条回答 默认 最新

  • douluohan3403 2012-11-01 14:11
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端