PHP - failed to open stream: No such file or directory in

Ty Lertwichaiworawit

I am new to PHP and Objective-C, I have searched for the answer to this but everything seemed complicated and I could not understand. I am trying to upload an image file to my ftp server using PHP.

I used this code in my app for the image upload:

UIImage *myImage = [UIImage imageNamed:@"black_strip.png"];
    NSData *imageData = UIImagePNGRepresentation(myImage);
    // setting up the URL to post to
    NSString *urlString = @"http://www.myurl.net/GagVidApp/uploadProfImage.php";

    // setting up the request object now
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"returnString: %@", returnString);

Here is my php code:

<?php
$file = basename($_FILES['userfile']['name']);
$remote_file = basename($_FILES['userfile']['name']);
//$remote_file = 'readme.txt';

// set up basic connection
$ftp_server = "www.myurl.net";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypassword";
$conn_id = ftp_connect($ftp_server); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
 echo "successfully uploaded $file\n";
} else {
 echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);
?>

The return string I get in my app is:

returnString: Connected to www.myurl.net, for user myuser<br />
<b>Warning</b>:  ftp_put(ipodfile.png) [<a href='function.ftp-put'>function.ftp-put</a>]: **failed to open stream: No such file or directory in** <b>/home/load2unet/domains/myurl.net/public_html/GagVidApp/uploadProfImage.php</b> on line <b>24</b><br />
There was a problem while uploading ipodfile.png

I have been trying to find the answer but haven't got any luck. Any help would be very much appreciated! Thanks

Rob

As Peter says, I suspect the problem is the failure to reference the full path of the temp file, namely, $_FILES["userfile"]["tmp_name"]. Here a rendition of what I've used in the past, using move_uploaded_file rather than ftp, but you probably get the idea:

<?php

header('Content-Type: application/json');

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfile"]["name"]));
if ((($_FILES["userfile"]["type"] == "image/gif")
     || ($_FILES["userfile"]["type"] == "image/jpeg")
     || ($_FILES["userfile"]["type"] == "image/png")
     || ($_FILES["userfile"]["type"] == "image/pjpeg"))
    && ($_FILES["userfile"]["size"] < 200000)
    && in_array($extension, $allowedExts))
{
    if ($_FILES["userfile"]["error"] > 0)
    {
        echo json_encode(array("error" => $_FILES["userfile"]["error"]));
    }
    else
    {
        if (file_exists("upload/" . $_FILES["userfile"]["name"]))
        {
            echo json_encode(array( "error" => $_FILES["userfile"]["name"] . " already exists"));
        }
        else
        {
            move_uploaded_file($_FILES["userfile"]["tmp_name"], "upload/" . $_FILES["userfile"]["name"]);
            echo json_encode(array("success"   => true,
                                   "upload"    => $_FILES["userfile"]["name"],
                                   "type"      => $_FILES["userfile"]["type"],
                                   "size"      => ($_FILES["userfile"]["size"] / 1024) . " kB",
                                   "stored in" => "upload/" . $_FILES["userfile"]["name"]));
        }
    }
}
else
{
    echo json_encode(array( "error" => "Invalid file"));
}
?>

Perhaps you don't need the file size check (or 200k might not be the right threshold), so that's up to you. But note that for programmatic interfaces, I format the response in JSON, so my app can easily parse the response.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP - Failed to open stream : No such file or directory

PHP/SQL - failed to open stream: No such file or directory

PHP Warning failed to open stream: No such file or directory

failed to open stream: No such file or directory

Failed to open stream: No such file or directory

File exists but PHP throwing "failed to open stream: no such file or directory"

Laravel classloader.php error failed to open stream: No such file or directory

PHP Warning: require_once: failed to open stream: No such file or directory in

include(MultiModelForm.php): failed to open stream: No such file or directory

Failed to open stream: No such file or directory in...? PHP Specific

yii PDO.php error: failed to open stream: No such file or directory

php artisan migrate:reset failed to open stream: No such file or directory

app/bootstrap.php.cache : failed to open stream: No such file or directory

include_once(): failed to open stream: No such file or directory - PHP

/autoload.php): failed to open stream: No such file or directory in

Warning: include(dbconn.php): failed to open stream: No such file or directory

Laravel include(DatabaseSeeder.php): failed to open stream: No such file or directory

failed to open stream: No such file or directory Codeignter

failed to open stream: No such file or directory (Laravel)

failed to open stream: No such file or directory issue

failed to open stream: No such file or directory on Homestead environment

PHP failed to open Stream no such file or directory when trying to execute a php file

fopen: failed to open stream: No such file or directory - but file/directory exists

ErrorException in Filesystem.php line 111:file_put_contents(): failed to open stream: No such file or directory

WordPress - PHP Warning failed to open stream: No such file or directory (advanced-cache.php)

PHP error with "logger.php" - Unknown: Failed to open stream: No such file or directory in Unknown on line 0

PHP - fopen(): Failed to Open Stream on uploaded file

include(MYPDF.php): failed to open stream: No such file or directory yii tcpdf

Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0 (php)