error upon uploading image no such path or directo

2020-03-06 01:38发布

问题:

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

回答1:

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

FTP_BINARY instead of FTP_ASCII.

As per the manual:

  • http://php.net/manual/en/function.ftp-put.php

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.

  • Usually 755.

    Use error reporting if your system isn't already setup to catch and display error/notices:

    • http://php.net/manual/en/function.error-reporting.php

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:

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


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.



标签: php forms