php - moving temp file after upload wont work -
before , know question posted thousand times none of them concern me since cant identify problem.
so ,i building php rest api put request client (angular), last can update file inside form text inputs , of sent form-data in code :
var fileformdata = new formdata(); $http({ method: 'put', url: host + '/produit/' + $scope.product.id, data: fileformdata, transformrequest: angular.identity, headers: { 'content-type': undefined } }).then(function successcallback(response) {}, function errorcallback(response) {});
on server-side have response type , parse data here's full code here
$method = $_server['request_method']; if ($method=="put"){ global $_put; /* put data comes in on stdin stream */ $putdata = fopen("php://input", "r"); /* open file writing */ // $fp = fopen("myputfile.ext", "w"); $raw_data = ''; /* read data 1 kb @ time , write file */ while ($chunk = fread($putdata, 1024)) $raw_data .= $chunk; /* close streams */ fclose($putdata); // fetch content , determine boundary $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n")); if(empty($boundary)){ parse_str($raw_data,$data); $globals[ '_put' ] = $data; return; } // fetch each part $parts = array_slice(explode($boundary, $raw_data), 1); $data = array(); foreach ($parts $part) { // if last part, break if ($part == "--\r\n") break; // separate content headers $part = ltrim($part, "\r\n"); list($raw_headers, $body) = explode("\r\n\r\n", $part, 2); // parse headers list $raw_headers = explode("\r\n", $raw_headers); $headers = array(); foreach ($raw_headers $header) { list($name, $value) = explode(':', $header); $headers[strtolower($name)] = ltrim($value, ' '); } // parse content-disposition field name, etc. if (isset($headers['content-disposition'])) { $filename = null; $tmp_name = null; preg_match( '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', $headers['content-disposition'], $matches ); list(, $type, $name) = $matches; //parse file if( isset($matches[4]) ) { //if labeled same previous, skip if( isset( $_files[ $matches[ 2 ] ] ) ) { continue; } //get filename $filename = $matches[4]; //get tmp name $filename_parts = pathinfo( $filename ); $tmp_name = tempnam( ini_get('upload_tmp_dir'), $filename_parts['filename']); //populate $_files information, size may off in multibyte situation $_files[ $matches[ 2 ] ] = array( 'error'=>0, 'name'=>$filename, 'tmp_name'=>$tmp_name, 'size'=>strlen( $body ), 'type'=>$value ); //place in temporary directory file_put_contents($tmp_name, $body); } //parse field else { $data[$name] = substr($body, 0, strlen($body) - 2); } } } $globals[ '_put' ] = $data; $input= array_merge($_put, $_files); }
and have execute query , move file form temp folder
$sql = "update `$table` set $set id=$key"; //works if(isset($name)){ //to check if file exist chmod($tmpname,0666); //since that's answer of question similar mine echo substr(sprintf('%o', fileperms($tmpname)), -4); //returns 0666 echo $tmpname." ".$name; //works if(move_uploaded_file($tmpname, "/home/****/****/uploads/produits/".$name)) echo "moved"; else echo "not moved"; // ,
can explain me please whats wrong ?
ps: checked /tmp
folder files uploaded still there.
log clean , used in top of code
ini_set('display_errors', true); error_reporting(e_all);
update :
i use file params $input
var
$values = array_map(function ($value) use ($link) { global $method; if ($value === null) return null; if (gettype($value) === "array"){ if ($method=="put" || $method=="post"){ global $tmpname,$name; $tmpname=$value['tmp_name']; $name=uniqid().$value['name']; $value=$name; } } return mysqli_real_escape_string($link, (string) $value); }, array_values($input));
Comments
Post a Comment