我正在尝试在我的网站上构建图像上传。我正在使用的代码将值插入到我的数据库中,但图像没有移动到我的目录中?
任何人都可以帮助我,或者给我如何调试的建议吗?
输入
<label for="picture_1">picture 1 : </label>
<input type="file" name="picture_1" id="picture_1" />
</li>
<li>
<label for="picture_2">picture 2 : </label>
<input type="file" name="picture_2" id="picture_2" />
</li>
<li>
<label for="picture_3">picture 3 : </label>
<input type="file" name="picture_3" id="picture_3" />
上传文件
if(sizeof($_FILES)){
for($i = 1; $i <= 3; $i++) {
$aFile = $_FILES['picture_'.$i];
if(empty($aFile['tmp_name'])) continue; # skip for empty elements
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $aFile["name"]));
if ((($aFile["type"] == "image/gif")
|| ($aFile["type"] == "image/jpeg")
|| ($aFile["type"] == "image/png")
|| ($aFile["type"] == "image/pjpeg"))
&& ($aFile["size"] < 200000000)
&& in_array(strtolower($extension), $allowedExts))
{
if ($aFile["error"] > 0)
{
echo "Return Code: " .$aFile["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $aFile["name"]))
{
echo $aFile["name"] . " already exists. ";
}
else
{
move_uploaded_file($aFile['tmp_name'],
"upload/" .$aFile["name"]);
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
完成页面
include 'assets/connection.class.php';
$firstname = $_POST['First_Name'];
// Connection data (server_address, database, name, poassword)
$hostdb = 'localhost';
$namedb = '';
$userdb = '';
$passdb = '';
try {
// Connect and create the PDO object
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT STMT WAS HERE";
$statement = $conn->prepare($sql);
$statement->bindValue(":firstname", $firstname);
...
List of bind values etc is here
$count = $statement->execute();
$conn = null; // Disconnect
}
catch(PDOException $e) {
echo $e->getMessage();
}
if(sizeof($_FILES)){
for($i = 1; $i <= 3; $i++) {
$aFile = $_FILES['picture_'.$i];
if(empty($aFile['tmp_name'])) continue; # skip for empty elements
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $aFile["name"]));
if ((($aFile["type"] == "image/gif")
|| ($aFile["type"] == "image/jpeg")
|| ($aFile["type"] == "image/png")
|| ($aFile["type"] == "image/pjpeg"))
&& ($aFile["size"] < 200000000)
&& in_array(strtolower($extension), $allowedExts))
{
if ($aFile["error"] > 0)
{
echo "Return Code: " .$aFile["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $aFile["name"]))
{
echo $aFile["name"] . " already exists. ";
}
else
{
move_uploaded_file($aFile['tmp_name'], "'upload/" .$aFile["name"]."'");
echo "Image Uploaded Successfully";
}
}
}
else
{
echo "Invalid file";
}
}
}
// If data added ($count not false) displays the number of rows added
if($count !== false) echo 'Number of rows added: '. $count;
?>
<a href="test.php">Go back </a>
错误日志
[Tue Aug 13 19:27:31 2013] [error] [client 89.240.60.62] PHP Warning: move_uploaded_file('upload/13-001945_1.jpg'): failed to open stream: No such file or directory in /home/liam.co.uk/domains/tiercakes.co.uk/public_html/access/add-cake.php on line 132, referer: http://www.tiercakes.co.uk/access/add-cake.php
[Tue Aug 13 19:27:31 2013] [error] [client 89.240.60.62] PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpxrwtlM' to ''upload/13-001945_1.jpg'' in /home/liam.co.uk/domains/tiercakes.co.uk/public_html/access/add-cake.php on line 132, referer: http://www.tiercakes.co.uk/access/add-cake.php