image upload with sending content with form
ba$lik:
spot kisa:
spot uzun:
kategori: ".$kat[isim].""; } ?> tiklanma开发者_运维问答:
tarih: " name="tarih" /> içerik:and post.php values:
mysql_query("INSERT INTO yazilar (baslik, spot, spot_kisa, icerik, kategori, tiklanma, eklemetarihi)
VALUES
('$_POST[baslik]','$_POST[spot]','$_POST[spot_kisa]','$_POST[icerik]','$_POST[kategori]','$_POST[tiklanma]','$_POST[tarih]')");
i need to add 2 picture links to table with upload form and insert db with post.php. two thumbnails with different size.
thanks!!
There are multiple valid approaches that can solve your question. I'm gonna show the easiest one I can think of.
Your form must have these modifications:
Add the ENCTYPE attibute to the form tag:
<form action="post.php" method="post" enctype="multipart/form-data">
Insert the file upload boxes (you could use a control array, like
image[]
, but I'll keep it simple):Image 1: <input type="file" name="image1" />
Image 2: <input type="file" name="image2" />
Keep in mind that sending files through a POST form implies that the upload size can't be larger than 2,048 Kb (2 megabytes).
Have yourself a directory prepared to receive the files, it should have the proper permissions set so users can write in your server (if it's a Linux server, you can use chmod 777). In this example my folder will be called "user-images".
Then, in post.php you add this:
$upload_to = "./user-images/";
move_uploaded_file(
$_FILES["image1"]["tmp_name"],
$upload_to . "/" . $_FILES["image1"]["name"]
);
move_uploaded_file(
$_FILES["image2"]["tmp_name"],
$upload_to . "/" . $_FILES["image2"]["name"]
);
?>
About resizing the images to generate thumbnails, you can use GD library to resize and store the files or you can do live thumbnails (also using GD library, but instead of loading a JPEG file in your IMG tag on the HTML display, you'd load a PHP file like this:)
<img src="thumbnail.php?src=image1.jpg&w=90&h=50" />
The links to the image files are easily generated:
$link = $upload_to . "/" . $_FILES["image1"]["name"]
If you need an absolute URL, simply add your domain address before $upload_to
.
This is just one way of doin' it. You can also add the binary content of the images to the database (since we're talking of thumbnails, we shouldn't have a problem, MySQL queries are limited to 1 Mb length, by default). This can be done using BLOB fields in the database table and file_get_contents()
(with PHP 5) or fopen()
in binary mode / fread()
/ fclose()
(with PHP 4) to read the files and dump them into the database. It's always suggested to encode the files so the SQL query isn't corrupted (usually you can do this with base64_encode()
or base64_decode()
).
Hope this can help you. Good luck coding!
精彩评论