>  기사  >  백엔드 개발  >  컬을 기반으로 한 PHP7 이미지 업로드 기능

컬을 기반으로 한 PHP7 이미지 업로드 기능

不言
不言원래의
2018-06-01 11:53:341736검색

이 글에서는 주로 컬을 기반으로 PHP7에서 구현한 이미지 업로드 기능을 소개하고, php5.5와 php7 버전 이전의 컬 이미지 업로드 기능에 대한 관련 구현 및 활용 스킬을 예시 형태로 비교 분석해 도움이 필요한 친구들이 참고할 수 있도록 했습니다. 이 글의 예제에

컬을 기반으로 PHP7에서 구현한 이미지 업로드 기능을 설명합니다. 참고하실 수 있도록 자세한 내용은 다음과 같습니다.

PHP 버전에 따라 컬 시뮬레이션 양식 업로드 방법이 다릅니다.

Before php5.5

$curl = curl_init();
if (defined('CURLOPT_SAFE_UPLOAD')) {
  curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
}
$data = array('file' => '@' . realpath($path));//‘@' 符号告诉服务器为上传资源
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1 );
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT,"TEST");
$result = curl_exec($curl);
$error = curl_error($curl);

After php5.5 , to php7

$curl = curl_init();
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
$data = array('file' => new \CURLFile(realpath($path)));
url_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1 );
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT,"TEST");
$result = curl_exec($curl);
$error = curl_error($curl);

호환되는 방법이 아래에 제공됩니다:

$curl = curl_init();
if (class_exists('\CURLFile')) {
 curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
$data = array('file' => new \CURLFile(realpath($path)));//>=5.5
} else {
 if (defined('CURLOPT_SAFE_UPLOAD')) {
  curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
 }
 $data = array(&#39;file&#39; => &#39;@&#39; . realpath($path));//<=5.5
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1 );
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT,"TEST");
$result = curl_exec($curl);
$error = curl_error($curl);

여기서:

$path: 업로드할 이미지 주소입니다.

$url: 목표 서버 주소

예를 들어

$url="http://localhost/upload.php";
$path = "/bg_right.jpg"

upload.php 예:

<?php
  file_put_contents(time().".json", json_encode($_FILES));
  $tmp_name = $_FILES[&#39;file&#39;][&#39;tmp_name&#39;];
  $name = $_FILES[&#39;file&#39;][&#39;name&#39;];
  move_uploaded_file($tmp_name,&#39;audit/&#39;.$name);
?>

관련 권장 사항:

PHP는 다중 이미지 업로드 및 단일 이미지 업로드 기능을 구현합니다

PHP pdo 기반 데이터베이스 작업 클래스 [mysql, sqlserver 및 oracle 지원 가능]

PHP는 양식의 반복 제출을 방지하는 기능을 구현합니다(토큰 검증 기반)

위 내용은 컬을 기반으로 한 PHP7 이미지 업로드 기능의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.