Modify the upload image? Solution Solutions

Source: Internet
Author: User
Modify the upload image?

Select a picture address:


I can upload images, can be modified to become a problem, because the image is stored in binary stream blob in the form of a database, so do not know how to read this address, that is, when I modified the image name does not modify the address, there is no change in the database image address

------Solution--------------------
Picture into 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 ($st 1, $st 2) =explode ("", Microtime ());
$startTime = $st 1+ $st 2;
$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 ($st 1, $st 2) =explode ("", Microtime ());
$time 2= $st 1+ $st 2;
echo ' Execution time '. ($time 2-$startTime);
?>


Recommendation: The binary data is Base64 encoded before joining the database, or some special symbols will cause the insertion to be unsuccessful.
------Solution--------------------
Upload processing:

Since the uploaded image is saved in a temporary file,
We just need to read the file to get a picture of the transmission.
$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 ");
Performs an insert operation and saves the result in a variable
$result = $dbh->query ("INSERT into employees
(firstname,lastname,emptype,age,picture) VALUES
(' Myfirst ', ' mylast ', ' myemptype ', ' $buf ') ');
Gets the number of rows affected
if ($result->rowcount () >0) {
Echo ("The data is inserted. ");
} else {
Echo ("Cannot perform insert operation. ");
}
Explicitly close the PDO connection
$DBH = NULL;
?>

------Solution--------------------
The existence of the database as usual to modify the text just do it ...
When he's a string, you can.
------Solution--------------------
Header ("Content-type:image/jpeg"); Add this to the front of the output.
  • 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.