php提示上传成功,froala图片明明上传成功了,还是会提示,遇到些错误,请在试试,原来还是php控制器的错误...

最终发现是php的问题

正确的php如下

public function fr_upload(){

try {

// File Route.

$fileRoute = ROOT_PATH . ‘public/uploads/‘;

$fieldname = "multipartFile";

// Get filename.

$filename = explode(".", $_FILES[$fieldname]["name"]);

// halt($filename);

// Validate uploaded files.

// Do not use $_FILES["file"]["type"] as it can be easily forged.

$finfo = finfo_open(FILEINFO_MIME_TYPE);

// Get temp file name.

$tmpName = $_FILES[$fieldname]["tmp_name"];

// Get mime type.

$mimeType = finfo_file($finfo, $tmpName);

// Get extension. You must include fileinfo PHP extension.

$extension = end($filename);

// Allowed extensions.

$allowedExts = array("gif", "jpeg", "jpg", "png", "svg", "blob");

// Allowed mime types.

$allowedMimeTypes = array("image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png", "image/svg+xml");

// Validate image.

if (!in_array(strtolower($mimeType), $allowedMimeTypes) || !in_array(strtolower($extension), $allowedExts)) {

throw new \Exception("File does not meet the validation.");

}

// Generate new random name.

$name = sha1(microtime()) . "." . $extension;

// halt( dirname(__FILE__));

$fullNamePath = $fileRoute . $name;

// Save file in the uploads folder.

move_uploaded_file($tmpName, $fullNamePath);

// halt($fullNamePath);

// // Check server protocol and load resources accordingly.

if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] != "off") {

$protocol = "https://";

} else {

$protocol = "http://";

}

//按时间分类存储,如果文件夹不存在则建立; 这里必须加上----$this->,是为了控制器内部的方法互相调用

// 由于是在uploads目录下创建,刚开始,我没写uploads,所以,文件地址一直不对。

$fileNewPath=$this->fileTime($fullNamePath);

if(!file_exists(‘uploads/‘.$fileNewPath)){

mkdir(‘uploads/‘.$fileNewPath);

}

// halt($fileNewPath);

$xinFullNamePath=$fileRoute.$fileNewPath.‘/‘.$name;

// halt($fullNamePath);

// halt($xinFullNamePath);

// 保存在新的地址里去

$this->moveFile($fullNamePath,$xinFullNamePath);

// Generate response.

$response = new \StdClass;

//$response->link = ‘uploads/‘.$fileNewPath.‘/‘.$name; 相对路径不行。必须是绝对路径

$response->link =$protocol.$_SERVER["HTTP_HOST"].‘/uploads/‘.$fileNewPath.‘/‘.$name;

//$response->link = ‘http://www.wrsks.com/statics/images/dk/logo.jpg‘;

// Send response.

echo stripslashes(json_encode($response));

} catch (Exception $e) {

// Send error response.

echo $e->getMessage();

http_response_code(404);

}

}

// 按时间进行存储图片-----

//取得文件修改时间

public function fileTime($str){

return date("Ymd",filemtime($str));

}

// //移动文件至新地址,其中第二个路径,不是目录,是新的图片地址

public function moveFile($file,$path){

copy($file,$path);

unlink($file);

}

// 按时间进行存储图片-----

原文:https://www.cnblogs.com/cn-oldboy/p/13237379.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值