SAE Storage: Use the storage service provided by the Sina SAE in PHP to upload a picture of your blog
Last Update:2017-02-28
Source: Internet
Author: User
This blog using Kindeditor4 as a background article editing and publishing, Kindeditor download package has about PHP upload file instances, directly using the example can be implemented local files uploaded to the server disk, but because the Sina SAE does not allow disk operations, So using the storage API for uploading files, and ke all the code comments in the upload_json.php file on disk operations, using the storage API, my final code looks like this:
<?php
/**
* Kindeditor PHP
*
* This PHP program is a demo program, it is not recommended to use directly in the actual project.
* If you are sure to use this program directly, please carefully confirm the relevant security settings before use.
*
*/
Require_once ' json.php ';
$php _path = dirname (__file__). '/';
$php _url = dirname ($_server[' php_self ')). '/';
File Save directory path
$save _path = $php _path. '.. /.. /res/upload/';
File Save Directory URL
$save _url = $php _url. '.. /.. /res/upload/';
Define file extensions to allow uploads
$ext _arr = Array (' Image ' => array (' gif ', ' jpg ', ' jpeg ', ' PNG ', ' bmp '), ' Flash ' => array (' SWF ', ' flv '), ' media ' => a Rray (' swf ', ' flv ', ' mp3 ', ' wav ', ' WMA ', ' WMV ', ' mid ', ' avi ', ' mpg ', ' asf ', ' rm ', ' rmvb ', ' file ' => array (' Doc ', ' docx ', ' xls ', ' xlsx ', ' ppt ', ' htm ', ' html ', ' txt ', ' zip ', ' rar ', ' gz ', ' bz2 ', ' gif ', ' jpg ', ' jpeg ', ' PNG ', ' BMP ',
);
Maximum file size
$max _size = 1000000;
$save _path = Realpath ($save _path). '/';
When there are uploaded files
if (empty ($_files) = = False) {
Original file name
$file _name = $_files[' imgfile ' [' name '];
Temporary file name on server
$tmp _name = $_files[' imgfile '] [' tmp_name '];
Alert (Please select a file.) " . $tmp _name);
File size
$file _size = $_files[' imgfile '] [' size '];
Check file name
if (! $file _name) {
Alert (Please select a file.) ");
}
$dir _name = Empty ($_get[' dir '])? ' Image ': Trim ($_get[' dir ']);
Get file name extension
$temp _arr = Explode (".", $file _name);
$file _ext = Array_pop ($temp _arr);
$file _ext = Trim ($file _ext);
$file _ext = strtolower ($file _ext);
Check extension
if (In_array ($file _ext, $ext _arr[$dir _name]) = = False) {
Alert ("The upload file name extension is an disallowed extension.") \ n only allow ". Implode (",", $ext _arr[$dir _name]). Format ");
}
New file name
$new _file_name = Date ("Ymdhis"). '_' . RAND (10000, 99999). '.' . $file _ext;
Moving files
$s = new saestorage ();
$result = $s->upload (' article ', $new _file_name, $tmp _name);
if (! $result) {
Alert ("Upload file failed.") ");
}
@chmod ($file _path, 0644);
$file _url = $result;
$s->geturl (' redstones ', $new _file_name);
Header (' content-type:text/html; Charset=utf-8 ');
$json = new Services_json ();
echo $json->encode (Array (' ERROR ' => 0, ' url ' => $file _url));
Exit
}
Function alert ($msg) {
Header (' content-type:text/html; Charset=utf-8 ');
$json = new Services_json ();
echo $json->encode (Array (' ERROR ' => 1, ' message ' => $msg));
Exit
}
?> This article links http://www.cxybl.com/html/wlbc/Php/20130601/38186.html