php 跨域上传图片 move_uploaded_file 返回false(踩坑日记)

问题描述:

提示:跨域上传图片 move_uploaded_file 返回false:
例如:在a.com下请求a.com a.php接口 a.com下a.php 接受file文件 然后通过a.php 请求 b.com 下的 b.php 在b.php总进行图片上传操作

@a.php	
		//设置请求参数
        if(isset($_REQUEST['uploadImgFun'])){
			$post_data['file'] = $_FILES['file'];
			$post_data['fun'] = $_REQUEST['uploadImgFun'];
			$post_data['kname'] = $_REQUEST['kname'];
		}
		$cookie = $_COOKIE;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// 我们在POST数据哦!
		curl_setopt($ch, CURLOPT_POST, 1);
		// 把post的变量加上
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
		curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		$output = curl_exec($ch);
		curl_close($ch);
		echo $output;
@b.php
	//接口接受文件
	if($_REQUEST['fun'] == "uploadFile3")
    {
        $file = $_REQUEST['file'];
        //调用图片上传方法
        echo ActivityUploadPic($file);
    }
    function ActivityUploadPic($file,$chycy,$name,$kname)
	{
    $date = date('Y-m-d');
    $filename = time().rand(1000,9999);
    preg_match('|\.(\w+)$|', $file["name"], $ext);

    $ext = strtolower($ext[0]);
	//限制图片上传格式
    if (!in_array($file["type"], array("image/gif","image/jpeg","image/pjpeg","image/jpg", "image/png"))) {
        return array("code" => 2, "msg" => "上传文件格式不正确");
    }
	//限制图片上传大小
    if ($file["error"] > 0) {
        return array("code" => 2, "msg" => "上传错误");
    }

    $path = ""; //上传路径
    //判断路径是否存在
    if(is_dir($path)){
        @unlink($path . $ext);
    }else{
    //创建路径 给最高权限
        mkdir(iconv("UTF-8","GBK",$path),0777,true);
    }
    $result = move_uploaded_file($file["tmp_name"], $path . "/".$filename.$ext);
    if($result)
    {
        $animal = array('code'=>1,'msg'=>'','count'=>'','data'=>"");//成功时返回 
        return json_encode($animal);
    }else
    {
        $animal = array('code'=>0,'msg'=>$result,'count'=>'','data'=>"");//失败时返回
        return json_encode($animal);
    }
}

原因分析:

提示:move_uploaded_file一直返回false:
分析:

  1. 文件传输是否有问题
  2. 是否是权限问题

解决方案:

  1. 对比上传文件参数
@正常请求返回接受的参数
	array (size=5)
	  'name' => string 'u=1951776306,1562297874&fm=26&gp=0.jpg' (length=38)
	  'type' => string 'image/jpeg' (length=10)
	  'tmp_name' => string 'C:\Users\Administrator\AppData\Local\Temp\php1245.tmp' (length=53)
	  'error' => int 0
	  'size' => int 25195
@跨域请求返回接受的参数
	 'file' => 
    array (size=5)
      'name' => string 'u=1951776306,1562297874&fm=26&gp=0.jpg' (length=38)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'C:\Users\Administrator\AppData\Local\Temp\php12DC.tmp' (length=53)
      'error' => string '0' (length=1)
      'size' => string '25195' (length=5)

通过参数对比我没发现有啥问题 跨域能正常接受文件 就是不能上传
通过php文档可知:
如果 file 不是合法的上传文件,不会出现任何操作,move_uploaded_file() 将返回 false。
如果 file 是合法的上传文件,但出于某些原因无法移动,不会出现任何操作,move_uploaded_file() 将返回 false,此外还会发出一条警告。
之后百度得出跨域上传文件需要通过form表单提交 但是form表单提交后有刷新 我没有进行测试,还有好多方法 比如ftp之类的

解决:

直到我看到这篇文章
把图片转换成二进制 然后在通过文件写入的方式进行图片保存,很适合我现在的情况(改动的不需要太多)

@解决代码
//获取后缀
    $type = explode('.',$file['name']);
    $image = $file['tmp_name'];
    //只读方式打开图片文件
    $fp = fopen($image, "r");
    //读取文件(可安全用于二进制文件)
    $file = fread($fp, $file["size"]); //二进制数据流

    //获取时间
    $date = date('Y-m-d');
    //上传路径
    $dir_url = "";//路径
    //检测目录是否存在,不存在则建立目录
    if (is_dir($dir_url)) {

    } else {
        mkdir(iconv("UTF-8","GBK",$dir_url), 0777, true);
    }
    // 重新编辑文件名
    $filename = time().rand(1000,9999);
    //新图片的路径
    $newFilePath = $dir_url.'/'.$filename.'.'.$type[1];
    $data = $file;
    $newFile = fopen($newFilePath,"w"); //打开文件准备写入
    $result = fwrite($newFile,$data); //写入二进制流到文件
    fclose($newFile); //关闭文件
    if($result)
    {
        $animal = array('code'=>1,'msg'=>'','count'=>'','data'=>"{$dir_url}/{$newFilePath}");
        return json_encode($animal);
    }else
    {
        $animal = array('code'=>0,'msg'=>$result,'count'=>'','data'=>"{$dir_url}/{$newFilePath}");
        return json_encode($animal);
    }

完美解决 perfect

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值