基本上我已经编写了一个脚本来允许后端的用户为画廊上传图片。该脚本应该将文件上传到服务器,然后将文件名和信息发布到数据库中。
它总是将文件上传到服务器而不会失败,但是由于某种原因,它只是偶尔将其发布到数据库中。有时它工作正常,但 10 次中有 8 次上传文件,仅此而已,脚本如下。
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$caption=$_POST['caption'];
$pic=($_FILES['photo']['name']);
$live=$_POST['live'];
//Connecting to the database
require_once('../Connections/tim.php');
//Writes the information to the database
mysql_query("INSERT INTO `gallery` VALUES ('$name', '$caption', '$pic', '$live')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded successfully, press back to upload more";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>