Modify Upload image? Solution-php Tutorial

Source: Internet
Author: User
Modify Upload image? & Lt; tr & gt; & nbsp; & lt; tdwidth & quot; 15% & quot; & gt; select the image address: & lt; td & gt; & nbsp; & lt; tdwidth & quot; 85% & quot; & gt; & lt; inputtype & quot; file & quot; name & quot; myphoto & quot; & g modify the upload image?

Select Image address:


I can upload images and modify them. The problem is that the images are stored in the database in the form of binary stream blob, so I don't know how to read this address, the image address in the database remains unchanged when the image name is modified and the address is not modified.

------ Solution --------------------
Convert image to binary

$ Img_file = 'xxx.jpg ';
$ Fp = fopen ($ img_file, 'RB ');
$ Content = fread ($ fp, filesize ($ img_file); // binary data
Fclose ($ fp );



Query display:
Header ("content-type: image/jpeg ");
List ($ st1, $ st2) = explode ("", microtime ());
$ StartTime = $ st1 + $ st2;
// $ PicContent = file_get_contents ("1.jpg ");
$ Lnk = mysql_connect ("localhost", "root", "123456 ");
Mysql_select_db ("test ");
// $ PicContents = base64_encode ($ picContent );
// $ Re = mysql_query ("insert into pic values (NULL, '$ picContents ')");
$ Re = mysql_query ("select * from pic ");
! $ Re & die ("query failed !~ ");
$ Rs = mysql_fetch_array ($ re );
Mysql_close ($ lnk );
Echo base64_decode ($ rs ['Pic ']);
List ($ st1, $ st2) = explode ("", microtime ());
$ Time2 = $ st1 + $ st2;
Echo 'execution Time'. ($ time2-$ startTime );
?>


Suggestion: encode binary data with base64 before joining the database. otherwise, some special symbols may cause insertion failure.
------ Solution --------------------
Upload processing:

// Because the uploaded image is saved in a temporary file
// You only need to read the file to obtain the uploaded image.
$ Fp = fopen ($ _ FILES ["myFile"] ["tmp_name"], "rb ");
$ Buf = addslashes (fread ($ fp, $ _ FILES ["myFile"] ["size"]);
// Create a PDO object
$ Dbh = new PDO ("mysql: host = localhost; port =
3306; dbname = myDatabase "," root "," verysecret ");
// Execute the insert operation and save the result in a variable
$ Result = $ dbh-> query ("insert into employees
(FirstName, lastName, EmpType, age, picture) VALUES
('Myfirst', 'myla', 'myemptype ', 50,' $ buf ')");
// Obtain the number of affected rows
If ($ result-> rowCount ()> 0 ){
Echo ("data inserted. ");
} Else {
Echo ("The INSERT operation cannot be performed. ");
}
// Explicitly close the PDO connection
$ Dbh = NULL;
?>

------ Solution --------------------
If a database exists, you can modify the text as usual...
It can be a string.
------ Solution --------------------
Header ("content-type: image/jpeg"); add

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.