我得到了这个 PHP 代码:
SESSION_start();
$conn = mysql_connect("localhost", "root", "");
if(!$conn) die("Cannot connect to the database server: <br />
".mysql_error());
$db = mysql_select_db("tsismics", $conn);
if(!$db) die("Cannot connect to the database: <br />
".mysql_error());
$userId = $_SESSION['userId'];
$dir = $userId."/".$_FILES['profpic']['name'];
$filename = $_FILES['profpic']['name'];
$target = "profile_pictures/".$userId."/";
echo $target;
mysql_query("UPDATE users SET photo=\"$dir\" WHERE userId=$userId", $conn);
if(move_uploaded_file($_FILES['profpic']['tmp_name'], $target)) {
echo "The file ". basename( $_FILES['profpic']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
这是我的 HTML 块:
<form method="POST" action="upload-prof-pic.php" enctype="multipart/form-data">
<input type="file" value="upload profile pic" name="profpic"/>
<input type="submit" value="Upload" id="prof-pic-submit">
</form>
上传时,它会在文件夹中创建一个以 userId 作为文件名且没有任何扩展名的文件:profile_pictures
我忘了发布这个..它有这两个错误:
警告:move_uploaded_file(profile_pictures/1/3.png):无法打开流:第 22 行的 C:\wamp\www\project\upload-prof-pic.php 中没有这样的文件或目录
警告:move_uploaded_file(): Unable to move 'C:\wamp\tmp\php2070.tmp' to 'profile_pictures/1/3.png' in C:\wamp\www\project\upload-prof-pic.php on line 22