【发布时间】:2016-03-04 07:42:27
【问题描述】:
您好,我需要一些帮助来将图片上传到我的网站,我已经更改了这个脚本几次,但我就是无法让它正常运行?
当我尝试上传任何图像时,脚本似乎失败并且没有更新记录以及没有上传图像?我对脚本做错了吗?
if(isset($_POST['uploadimage'])){
$user = $_POST['user'];
$target_dir = "userimg/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["fileToUpload"]);
if($check !== false) {
$uploadOk = 1;
} else {
$imgerror = 1;
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
$imgerror = 2;
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"] > 500000) {
$imgerror = 3;
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$imgerror = 4;
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$filenameimg = basename( $_FILES["fileToUpload"]);
mysqli_query($conn, "UPDATE friends_list SET display_img = '$filenameimg' WHERE id = '$user'");
$imgerror = 6;
} else {
$imgerror = 5;
}
}
}
【问题讨论】:
-
查看以下链接 php.net/manual/en/mysqli.error.php 和 php.net/manual/en/function.error-reporting.php 并将其应用于您的代码。
-
在代码运行时使用错误报告工具是否是个好主意,以防出现问题,如果我这样做,用户是否能够查看出现的错误?跨度>
-
只在测试时使用。是的,这些会出现在用户面前。如果你看到他们,他们也会;-)
-
下次遇到困难时我会记住这一点,非常感谢! :)
标签: php html mysql image uploading