Because to upload all uploaded images to a separate picture server above, JS upload there is a cross-domain problem, find this online, through the PHP curl, the picture is sent to another server to save, and return the picture Path!
There are some problems with this approach:
1, upload large pictures, such as 2M, you need to upload the image first to the server, and then to the image server, the middle needs 4M transmission, increase the upload time!
2, from the server to the picture server because there is no save, the transfer is using temporary files, taking into account the picture format restrictions, the image has been renamed, after the transfer of the image to delete, encountered some unknown problem is likely to cause the picture deletion failure, increase the server storage space!
The code is as follows:
[HTML]View PlainCopy
- Public function upload ($files = ') {
- if ("files = = =") {
- $files = $_files;
- }
- if (empty ($files)) {
- $this->error = ' no uploaded files! ‘;
- return false;
- }
- $ext = pathinfo (strip_tags ($files [' file '] [' name ']), pathinfo_extension);
- $new_name = dirname ($files [' file '] [' tmp_name ']). ' /'. Date ("Ymdhis"). Rand (0,99). ".". $ext;
- Rename ($files [' file '] [' tmp_name '], $new _name);
- $fields [' img '] = ' @ '. $new _name;
- Initialize Curl
- $ch = Curl_init ();
- curl_setopt ($ch, Curlopt_url, $this->url);
- curl_setopt ($ch, Curlopt_post, 1);
- curl_setopt ($ch, Curlopt_postfields, $fields);
- curl_setopt ($ch, Curlopt_returntransfer, TRUE);
- Run Curl
- $R = curl_exec ($ch);
- Curl_close ($ch);
- @unlink ($new _name);
- if ($r) {
- Return Json_decode ($r, true);
- }else{
- $this->error = "Request Failed";
- return false;
- }
- }
PHP cross-domain upload images