我是PHP的新手,我正在学习Headfirst PHP和MySQL的书。
我已经制作了一个HTML表单,因此用户可以上传文件(在本例中是一个图像文件)。 这是代码:
<?php
define('GW_UPLOADPATH', 'guitar/images/');
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$score = $_POST['score'];
$screenshot = $_FILES['screenshot']['name'];
if(!empty($name) && !empty($score) && !empty($screenshot)) {
$target = GW_UPLOADPATH . $screenshot;
if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {
$dbc = mysqli_connect('localhost', 'root', '', 'guitar')
or die('Error connecting to MySQL server');
$query = "INSERT INTO guitarwars VALUES (0, '$name', '$score', NOW(), '$screenshot')";
$result = mysqli_query($dbc, $query) or die('Error querying database.');
echo '<p>Thanks for adding your new high score!</p>';
echo '<p><strong>Name:</strong> ' . $name . '<br />';
echo '<strong>Score:</strong> ' . $score . '</p>';
echo '<img src="' . GW_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';
echo '<p><a href="index.php"><< Back to high scores</a></p>';
$name = "";
$score = "";
$screenshot = "";
mysqli_close($dbc);
}else{
echo '<p class="error">Sorry, there was a problem uploading your screen shot image.</p>';
}
}else{
echo '<p class="error">Please enter all of the information to add ' .
'your high score.</p>';
}
}
?>
当我把index.php&amp;服务器根文件夹中的images文件夹一切正常。
但是当我在htdocs文件夹(服务器根文件夹)中创建目录(在本例中为:guitar文件夹)并将项目文件放在该目录中时,问题就出现了。
问题是,当用户上传图像文件时,它不会转到图像文件夹,并显示此错误:
Warning: move_uploaded_file(guitar/images/jacobsscore.gif): failed to open stream: No such file or directory in C:\xampp\htdocs\guitar\addscore.php on line 35
Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpE3BE.tmp' to 'guitar/images/jacobsscore.gif' in C:\xampp\htdocs\guitar\addscore.php on line 35