我正在尝试上传图片。 目的是在用户的个人资料上添加图片。 为了实现我做一个简单的例子,我从我的计算机更新图片,做一些检查,如果图像没问题,那么它将更新到'照片'文件夹(在cpanel内,public_html /帐户/照片)。 问题是一切看起来都很好,但是当我去cPanel内的那条路时,图片就不存在了。 我写错了路吗?
这是完整的代码:
HTML
<html>
<body>
<form action="/My-code/upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
upload.php的
<?php
$target_dir = "Account/photos/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
echo $target_file;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
echo "wally1 " . $_FILES["fileToUpload"]["tmp_name"];
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $_FILES["fileToUpload"]["name"])) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
什么想法我做错了什么? 而且,有可能做我想做的事情吗?
提前致谢
$target_dir = "/Account/photos/";
是来自服务器的根文件系统的绝对路径,它可能不存在,并且Web服务器用户(例如apache)将没有正确的权限来创建它。
您可能希望它与您网站的目录相关,因此请先尝试删除它/
您也没有将$target_file
分配给脚本顶部的任何内容。