php - 上传图片时出错没有这样的路径或目录

标签 php forms

美好的一天,我一直在学习教程并非常努力地使这个脚本工作,我已经设法解决了很多问题,但是我遇到的一个老问题是:

第 93 行/home/xxxxxxx/public_html/regForm.php 中没有这样的文件或目录

我想要完成的是让用户在注册时将个人资料图片上传到我的实时服务器/网站,这是我的表单的一部分和下面的代码:

如果更有经验的用户可以扫描一下并提前告知我哪里出错了,我将不胜感激

HTML 格式

 <form ENCTYPE="multipart/form-data" name="registration-form" id="regForm" method="post" action="regForm.php">
 :
 :
Upload Picture: <INPUT NAME="file_up" TYPE="file">
<input type="submit"  value="Register" name="register" class="buttono" />
</form>

上传脚本

$file_upload="true";
$file_up_size=$_FILES['file_up']['size'];
echo $_FILES['file_up']['name'];

if ($_FILES['file_up']['size']>250000){
    $msg=$msg."Your uploaded file size is more than 250KB
 so please reduce the file size and then upload.<BR>";
$file_upload="false";
}

if (!($_FILES['file_up']['type'] =="image/jpeg" OR $_FILES['file_up']['type'] =="image/gif"))
{
    $msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";
}

$file_name=$_FILES['file_up']['name'];

if($file_upload=="true"){

$ftp_server = "xxxxxx";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username='xxxxxx';
$ftp_userpass='xxxxxx';
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$file = $file_name;

// upload file
if (ftp_put($ftp_conn, "public_html/img/userPics", $file, FTP_ASCII)) //here is the problem with the path I suspect
  {
  echo "Successfully uploaded $file.";
  }
else
  {
  echo "Error uploading $file.";
  }

// close connection
ftp_close($ftp_conn);   
}

最佳答案

在处理图像时,需要使用二进制格式。

FTP_BINARY 而不是 FTP_ASCII。

根据手册:

确保路径正确。这可能因不同的服务器而异。

这里还需要一个尾部斜线:

public_html/img/userPics/
                        ^

否则,您的系统会将其解释为:

userPicsIMAGE.JPG 而不是预期的 userPics/IMAGE.JPG

然而,您可能需要尝试一下路径本身。 FTP 在这方面有点棘手。

即:

  • /home/xxxxxxx/public_html/userPics/
  • img/userPics/
  • ../img/userPics/

取决于文件的执行区域。

Root > ftp_file.php  
-img  
   -userPics

The folder also needs to have proper permissions to be written to.

An example from the FTP manual:

<?php 
ftp_chdir($conn, '/www/site/'); 
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY ); 
?>

还有一件事,“真”和“假”的使用。您很可能想检查真实性

$file_upload=true;

而不是字符串文字 $file_upload="true";

同样的事情

if($file_upload=="true")

if($file_upload==true)

并为 $file_upload="false"; 检查错误

$file_upload=false;

阅读手册:

This is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE.


测试您的 FTP 连接,从 https://stackoverflow.com/a/3729769/ 拉取

try {
    $con = ftp_connect($server);
    if (false === $con) {
        throw new Exception('Unable to connect');
    }

    $loggedIn = ftp_login($con,  $username,  $password);
    if (true === $loggedIn) {
        echo 'Success!';
    } else {
        throw new Exception('Unable to log in');
    }

    print_r(ftp_nlist($con, "."));
    ftp_close($con);
} catch (Exception $e) {
    echo "Failure: " . $e->getMessage();
}

命名约定:

我还需要注意,在 LINUX 上,userPicsuserpics 不同,如果您的文件夹名称是小写字母。

与不区分大小写的 Windows 不同。

关于php - 上传图片时出错没有这样的路径或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32389616/

相关文章:

php - 如何从多维数组中插入排序数据

php - 获取mysql中日期范围之间的行

php - OctoberCMS 不创建索引

python - 时间间隔选择的最佳界面范例?

jquery - 重力形式总字段在 WordPress 页面中不起作用

html - 表单域对齐

html - (如何)我可以在新窗口中打开表单提交的结果吗?

javascript - 在 Woocommerce 可变产品的 jQuery 中获取选定的变化价格

仅当下一个输入大于 1 时,Jquery 才乘以值

php - 为什么 fetch_assoc 返回 false?