PHP-无法打开流:在该目录中没有此类文件或目录 [英] PHP - failed to open stream: No such file or directory in

查看:75
本文介绍了PHP-无法打开流:在该目录中没有此类文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP和Objective-C的新手,我一直在寻找答案,但是一切似乎都很复杂,我无法理解.我正在尝试使用PHP将图像文件上传到我的ftp服务器.

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.

我在我的应用中使用此代码上传了图片:

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

这是我的php代码:

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

我在应用程序中获得的返回字符串是:

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

推荐答案

正如Peter所说,我怀疑问题是无法引用临时文件的完整路径,即 $ _ FILES ["userfile"]["tmp_name"] .这是我过去使用过的内容的再现,使用 move_uploaded_file 而不是ftp,但您可能会明白:

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

也许您不需要文件大小检查(或者200k可能不是正确的阈值),所以这取决于您.但是请注意,对于编程接口,我将响应格式设置为JSON,因此我的应用可以轻松解析响应.

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.

这篇关于PHP-无法打开流:在该目录中没有此类文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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