我正在开发一个使用数据库获取产品图像的网站。一切都很好,所以我想制作一个页面,轻松地将图像文件从本地桌面传输到我的ftp.server。现在发生了一件非常奇怪的事情,我不知道为什么。
在测试阶段,我一直在尝试一个特定的文件"理发.jpg"。只是下载了一张随机的图片。过了一段时间,页面工作了,我可以将文件"理发.jpg"上传到我的ftp服务器。
现在我想开始使用该页面将更多的图像上传到我的ftp服务器,但只有图像"理发.jpg"才能工作。我尝试的每一个其他图像都失败了,它给了我下一个错误:
警告:ftp_put(kabeouter.jpg):无法打开流:在…中没有这样的文件或目录
我真的不知道这可能是什么。我从ftp服务器上删除了"理发.jpg"文件,但我可以不断上传,它总是有效的。我尝试使用另一个浏览器;同样的问题还在继续。
其他文件在完全相同的文件夹中,大小大致相同(我尝试过越来越大)。我试过用.jpg的其他文件。我试过其他文件夹。不是工作,只是一个特定的"理发.jpg"工作。
我用于ftp传输的所有代码如下:
//FTP:
$ftp_server = "ftpserver";
$ftp_username = "username";
$ftp_userpass = "password";
$remote_dir = "/test/";
$target_file = $remote_dir.basename($_FILES['afbeelding']["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
//Setup basic connection
//ftp_connect(host, port [def=21], timeout [def=90])
$ftpConn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftpConn, $ftp_username, $ftp_userpass);
我认为下一块代码不会引起问题,但我无论如何都会发布它:
//Check if image file is a actual image or fake image
if(isset($_POST["submit"]))
{
$check = getimagesize($_FILES['afbeelding']["name"]);
if($check !== false){
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
else{
echo "File is not an image.";
$uploadOk = 0;
}
}
//Check if file already exists
if(in_array(basename($_FILES['afbeelding']["name"]), ftp_nlist($ftpConn, $remote_dir)))
{
echo "Sorry, file already exists.";
$uploadOk = 0;
}
//Check file size
if($_FILES['afbeelding']["size"] > 3000000)
{
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
//Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif")
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
这是FTP传输实际发生的情况:
//Check if $uploadOk is set to 0 by an error
if($uploadOk == 0){
echo "<br>Sorry, your file was not uploaded.";
}
//If everything is OK, try to upload the file
else
{
//Check if ftp transfer was succesfull
if(ftp_put($ftpConn, $target_file, basename($_FILES['afbeelding']["name"]), FTP_ASCII )){
echo "The file ". basename($_FILES['afbeelding']["name"]). " has been uploaded.";
}
else {
"Sorry, there was an error uploading your file.";
}
}
//Remember to always close the ftp connection
ftp_close($ftpConn);
我希望任何人都能认识到这个问题,并能帮助我。我现在一无所知。
$_FILES
数组的"name"
元素不包含可以读取的文件名!它是在用户的客户端机器上的文件名,而不是在服务器上。
文件的文件名位于"tmp_name"
元素中,上传的文件临时存储在服务器上。
您的PHP文件所在的文件夹中一定有一些"haircut.jpg"
的遗忘副本。
只需将用于访问文件的"name"
的所有实例替换为"tmp_name"
(随机名称)。保留(漂亮的)"name"
,在这里您可以处理FTP路径。
有关$_FILES
的结构,请参阅POST方法上传。