美好的一天,我一直在学习教程,并且为使该脚本正常工作付出了很大的努力,我设法解决了许多问题,但是我遇到的一个经常出现的问题是:
第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.
按照手册:
> http://php.net/manual/en/function.ftp-put.php
确保路径正确.这可能与不同的服务器不同.
您还需要在此处加上斜杠:
public_html/img/userPics/
^
否则,您的系统将其解释为:
userPicsIMAGE.JPG,而不是预期的userPics / IMAGE.JPG
但是,您可能需要尝试一下路径本身. FTP有点棘手.
即:
> / home / xxxxxxx / public_html / userPics /
> img /用户图片/
> ../img/userPics/
取决于文件的执行区域.
Root > ftp_file.PHP -img -userPics
该文件夹还需要具有适当的权限才能写入.
>通常为755.
如果您的系统尚未设置为捕获并显示错误/通知,请使用错误报告:
> http://php.net/manual/en/function.error-reporting.php
FTP手册中的一个示例:
<?PHP
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );
?>
但是,另一件事是使用“ true”和“ false”.您最有可能想要检查真实性
$file_upload=true;
而不是字符串文字$file_upload =“ true”;
同样的事情
if($file_upload=="true")
至
if($file_upload==true)
和$file_upload =“ false”;检查虚假
到$file_upload = false;
阅读此手册:
> http://php.net/manual/en/language.types.boolean.php
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上,如果您的文件夹名称使用小写字母,则userPics与userpics不同.
与Windows不同,它不区分大小写.