The client's file is uploaded to the server, and the server returns the path to the file

Source: Internet
Author: User

The client's file is uploaded to the server, the server returns the path to the file, and the client saves the file

Client:

<?php
Header (' Content-type:text/html;charset=utf8 ');
$url = ' http://192.168.1.118/legcc/aaa.php ';//The address of the server being accessed
$curl = Curl_init ();
$path = ' D:\www\ceshi\a02.jpeg ';//absolute path to the client file
$source = file_get_contents ($path);

$dir = Date (' y/m/d/', Time ());
$data = Array (' source ' = = $source, ' name ' = = ' A02 ', ' ext ' = = '. jpeg ', ' dir ' = = $dir);//Parameter
$re = curl_setopt ($curl, Curlopt_url, $url);
curl_setopt ($curl, Curlopt_returntransfer, true);
curl_setopt ($curl, Curlopt_post, true);
curl_setopt ($curl, Curlopt_postfields, $data);
$result = curl_exec ($curl);

Curl_close ($curl);
if (Json_decode ($result)->errrormessage ==1) {
Var_dump (Json_decode ($result));
} else if (Json_decode ($result)->errrormessage ==2) {
Echo ' file has been uploaded ~ ';
} else {
echo ' upload failed! ‘;
}

Service side:

<?php
The server receives the file that the client passes over to save
$basePath = ' e:\www\legcc/'; directory where files are stored
$path = $basePath. $_post[' dir '];
$results =[];
if (!is_dir ($path)) {
mkdir ($path, 0777,true);
}
if (!is_file ($path. $_post[' name '].$_post[' ext '))) {
$fuck = File_put_contents ($path. $_post[' name '].$_post[' ext '], $_post[' source ');
if ($fuck) {
$path 1 = ' http://192.168.1.118/legcc/'. $_post[' dir '. Rand (10,99). $_post[' ext '];
$results = [
errorcode=> $path 1,
Errrormessage=>1
];
}else{
$results = [
Errrormessage=>0
];
}
}else{
$results =[
Errrormessage=>2
];
}
echo Json_encode ($results);

The client's file is uploaded to the server, and the server returns the path to the file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.