上传图片时出错无此路径或目录 [英] error upon uploading image no such path or directory

查看:174
本文介绍了上传图片时出错无此路径或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,我一直在关注教程,并且非常努力地使这个脚本能够工作,我设法解决了很多问题,但是我得到的一个常量问题是:



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



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



如果一位更有经验的用户可以给它一个扫描并告知我错误的地方,我会非常感激,thanx提前



HTML FORM

 < form ENCTYPE =multipart / form-dataname =registration -formid =regFormmethod =postaction =regForm.php> 


上传图片:< INPUT NAME =file_upTYPE =file>
< input type =submitvalue =Registername =registerclass =buttono/>
< / form>

UPLOAD SCRIPT

  $ file_upload = 真; 
$ file_up_size = $ _ FILES ['file_up'] ['size'];
echo $ _FILES ['file_up'] ['name'];

if($ _FILES ['file_up'] ['size']> 250000){
$ msg = $ msg。您上传的文件大小超过250KB
所以请减小文件大小然后上传。< BR>;
$ file_upload =false; $!

$ b if(!($ _ FILES ['file_up'] ['type'] ==image / jpeg或$ _FILES ['file_up'] ['type'] = =image / gif))
{
$ msg = $ msg。您上传的文件必须是JPG或GIF,其他文件类型不允许使用< BR>;
$ file_upload =false;
}

$ file_name = $ _ FILES ['file_up'] ['name'];

if($ file_upload ==true){

$ ftp_server =xxxxxx;
$ ftp_conn = ftp_connect($ ftp_server)或死(无法连接到$ ftp_server);
$ ftp_username ='xxxxxx';
$ ftp_userpass ='xxxxxx';
$ login = ftp_login($ ftp_conn,$ ftp_username,$ ftp_userpass);

$ file = $ file_name;

//上传文件
if(ftp_put($ ftp_conn,public_html / img / userPics,$ file,FTP_ASCII))//这是我怀疑$ b的路径问题$ b {
echo成功上传$文件。;
}
else
{
echo上传$ file时出错。
}

//关闭连接
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 $ b取决于文件的执行区域。

$ b

该文件夹还需要有适当的权限才能写入。





一个来自FTP手册的例子:

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

另一件事是使用true和false。你很可能想要检查真实性/ b

  $ file_upload = true; 

而不是字符串文字 $ file_upload =true; $ b pre
$ b pre $ if $($ file_upload ==true)



<$如果($ file_upload == true)

并且对于

code $ file_upload =false; 检查是否存在错误

$ file_upload = false;



阅读此手册: http://php.net/manual/en/language.types.boolean。 PHP



这是最简单的类型。布尔值表示真值。它可以是TRUE或FALSE。






测试你的FTP连接, a href =https://stackoverflow.com/a/372​​9769/> https://stackoverflow.com/a/372​​9769/

  try {
$ con = ftp_connect($ server);
if(false === $ con){
抛出新的异常('Unable to connect');
}

$ loggedIn = ftp_login($ con,$ username,$ password);
if(true === $ loggedIn){
echo'Success!';
} else {
抛出新的异常('无法登录');
}

print_r(ftp_nlist($ con,。));
ftp_close($ con);
} catch(Exception $ e){
echoFailure:。 $ E->的getMessage();

$ / code>






命名惯例: p>

我还需要注意的是,在LINUX上, userPics 不同于 userpics ,您的文件夹名称应该是小写字母。



与Windows不区分大小写。


Good day, I have been following a tutorial and have worked really hard to make this script work, I have managed to fix many things however the one constant problem I'm getting is:

No such file or directory in /home/xxxxxxx/public_html/regForm.php on line 93

What I am trying to acomplish is get user to upload a profile pic to my live server / website upon registration, here is part of my form and the code below:

I would greatly appreciate it if a more experienced user can give it a scan and advise to where I am going wrong, thanx in advance

HTML FORM

 <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>

UPLOAD SCRIPT

$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);   
}

解决方案

When dealing with images, you need to use a binary format.

FTP_BINARY instead of FTP_ASCII.

As per the manual:

Make sure the path is correct. This can differ from different servers.

You also need a trailing slash here:

public_html/img/userPics/
                        ^

Otherwise, your system will interpret that as:

userPicsIMAGE.JPG rather than the intended userPics/IMAGE.JPG

Yet, you may need to play around with the path itself. FTP is a bit tricky that way.

I.e.:

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

Depending on the file's area of execution.

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 ); 
?>

Another thing though, the use of of "true" and "false". You're most likely wanting to check for truthness

$file_upload=true;

rather than a string literal $file_upload="true";

same thing for

if($file_upload=="true")

to

if($file_upload==true)

and for $file_upload="false"; to check for falseness

to $file_upload=false;

Read the manual on this:

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


Test your FTP connection, pulled from 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();
}


Naming conventions:

I also need to note that on LINUX, userPics is not the same as userpics, should your folder name be in lowercase letters.

Unlike Windows, which is case-insensitive.

这篇关于上传图片时出错无此路径或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆