Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Angular 8和PHP将图像上载到无法正常工作的服务器_Php_Angular_Xmlhttprequest_Image Uploading - Fatal编程技术网

使用Angular 8和PHP将图像上载到无法正常工作的服务器

使用Angular 8和PHP将图像上载到无法正常工作的服务器,php,angular,xmlhttprequest,image-uploading,Php,Angular,Xmlhttprequest,Image Uploading,我已经挣扎了好几个小时,现在试图上传一个图像到我的服务器使用PHP。我以前只在Angular 2中工作过,现在我改成了8,我似乎无法让我的旧代码正常工作,我怀疑这是因为Http方法更新到HttpClient 下面是app.component.ts,它使用输入文件调用服务: let fileslen = $("#images")[0].files.length; for(let i = 0; i < fileslen; i++){ this.files.push($("#images")

我已经挣扎了好几个小时,现在试图上传一个图像到我的服务器使用PHP。我以前只在Angular 2中工作过,现在我改成了8,我似乎无法让我的旧代码正常工作,我怀疑这是因为Http方法更新到HttpClient

下面是app.component.ts,它使用输入文件调用服务:

let fileslen = $("#images")[0].files.length;
for(let i = 0; i < fileslen; i++){
  this.files.push($("#images")[0].files[i]);
  this.serviceImage.uploadImage(upload_url, this.files[i])
        .subscribe(
            response=> {
              this.imagesUrl.push(response["image"]);
            },
            error=>{
              alert('error:' + JSON.stringify(error));
            }
        );
}
最后是php文件:

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

    $uploads_dir = 'uploads';
    $object = new stdClass();
    $errorMsg = "Error in image";
    $noFile = "No file";
    if(isset($_FILES['imagen'])){
        $tmp_name = $_FILES['image']['tmp_name'];
        $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
        $randNum = rand(0, 1000);
        $fileName = time() . sha1($_FILES['image']['name']. $randNum) . "." .$ext;
        if(move_uploaded_file($tmp_name, "$uploads_dir/$fileName")){
            $object->image = "$fileName";
            echo json_encode($object);
        }else{
            echo json_encode($errorMsg);
        }
    }else{
        echo json_encode($noFile);
    }
?>

有控制台错误吗?控制台中的请求-响应是什么样子的?如果有问题,需要正确解释。“不工作”是一种过于宽泛的描述。会发生什么?你做了什么调试?你的JS希望结果是JSON,但你只是在出错时按原样输出字符串。尝试将错误消息作为json返回。我这样做了,返回的错误是“img中的错误”,因此无法移动文件。确保您有正确的目标文件夹,并且web服务器运行时的用户可以写入该文件夹。您是否已检查web服务器错误日志,以查看是否收到任何可能解释此问题的错误消息?
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

    $uploads_dir = 'uploads';
    $object = new stdClass();
    $errorMsg = "Error in image";
    $noFile = "No file";
    if(isset($_FILES['imagen'])){
        $tmp_name = $_FILES['image']['tmp_name'];
        $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
        $randNum = rand(0, 1000);
        $fileName = time() . sha1($_FILES['image']['name']. $randNum) . "." .$ext;
        if(move_uploaded_file($tmp_name, "$uploads_dir/$fileName")){
            $object->image = "$fileName";
            echo json_encode($object);
        }else{
            echo json_encode($errorMsg);
        }
    }else{
        echo json_encode($noFile);
    }
?>
[Mon Sep 23 13:38:53.573923 2019] [proxy_fcgi:error] 
[pid 17680:tid 140606869325568] 
[client 213.127.0.0:37223] 
AH01071: Got error 'PHP message: PHP Warning:
move_uploaded_file(uploads/156923873339c6f3f02e62236530e6b49efbafabacd958828a.pdf): 
failed to open stream: 
No such file or directory in /var/www/clients/client490/web1568/web/app/php/upload_image.php 
on line 15\nPHP message: PHP Warning:  move_uploaded_file(): 
Unable to move '/var/www/clients/client490/web1568/tmp/phpIdHxdy' 
to 'uploads/156923873339c6f3f02e62236530e6b49efbafabacd958828a.pdf' 
in /var/www/clients/client490/web1568/web/app/php/upload_image.php 
on line 15\n', 
referer: {url}