无法使用PHP将图像上传到文件夹中

时间:2016-11-24 10:47:44

标签: php file file-upload image-uploading

我无法使用php将文件上传到目录中。我在下面解释我的代码。

$_FILES=array('picture'=>array('name' => 'IMG-20161121-WA0000.jpg','type' => ' image/png','tmp_name' => '/tmp/phpSb6a53', 'error' => 0, 'size' => 119198));
$imageFieldName = 'picture';
$imagePath = "admin/uploads/";
uploadImage($_FILES,$imageFieldName,$imagePath,function($image){
    print_r($image);exit;
}
public function uploadImage($files, $imageFieldName, $imageDirPath, $callback) {
        $result = array();
        ;
        $imageName = generateRandomNumber() . '_' . $_FILES[$imageFieldName]['name'];
       // echo($_FILES[$imageFieldName]['tmp_name']);exit;
        $target_dir = $imageDirPath;
        $target_file = $target_dir . basename($imageName);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
        if (file_exists($target_file)) {
            $result['msg'] = "Sorry, file already exists.";
            $result['num'] = 0;
            $callback($result);
            $uploadOk = 0;
        }
        if ($_FILES[$imageFieldName]["size"] > 500000) {
          //  echo 'fileSize';exit;
            $result['msg'] = "Sorry, file size is large.";
            $result['num'] = 0;
            $callback($result);
            $uploadOk = 0;
        }
        if (($imageFileType != "jpg" && $imageFileType != "JPG") && $imageFileType != "png" && $imageFileType != "jpeg") {
            $result['msg'] = "Sorry, only .jpg,.jpeg and .png files are allowed.";
            $result['num'] = 0;
            $callback($result);
            $uploadOk = 0;
        }
        if ($uploadOk == 0) {
            $result['msg'] = "Sorry, Your file could not uploaded.";
            $result['num'] = 0;
            $callback($result);
        } else {
            if (move_uploaded_file($_FILES[$imageFieldName]['tmp_name'], $target_file)) {
                $result['msg'] = "Image has uploaded successfully.";
                $result['num'] = 1;
                $result['img'] = $imageName;
                $callback($result);
            } else {
                $result['msg'] = "Sorry, Your Image could not uploaded to the directory.";
                $result['num'] = 0;
                $callback($result);
            }
        }
    }

选择文件后我

  

$_FILES=array('name' => 'IMG-20161121-WA0000.jpg','type' => ' image/png','tmp_name' => '/tmp/phpSb6a53', 'error' => 0, 'size' => 119198);

作为输入。

但是我收到了消息

  

`数组([msg] =>抱歉,您的图片无法上传到   目录。 [num] => 0)

而print_r。在这里,我需要将图像上传到文件夹中。请帮帮我。

1 个答案:

答案 0 :(得分:-1)

您是否检查过php.ini文件上传设置?

检查

的值
upload_max_filesize 
post_max_size 
memory_limit

也许它们太小了。