Page 1 of 1

how to upload images

Posted: Sun Sep 11, 2005 11:24 am
by atif
feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


im new to php. i want to user if the site to upload the images on the server and then these images are shown as thumbnail as soon as they are uploaded. the code that i wrote to upload the images is

upload.htm

Code: Select all

<html> 

<form action="myupload.php" method=post enctype="multipart/form-data"> 
submit this file: <input type=file name="userfile"><br> 
<input type=submit><br> 
</form> 
</html> 
myupload.php
<html> 
<? 
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of $_FILES. 
if(!empty($_FILES["userfile"])) { 
    $uploaddir = "/upload/" // set this to wherever 
    //copy the file to some permanent location 
    if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $uploaddir . $_FILES["userfile"]["name"])) {
        echo("file uploaded");
    } else {
        echo ("error!");
    }
} 
?> 
</html>
but as soon as i click the submit button i get the file save dialog .like we do get when we right click and select save target as.kindly help me how to upload the images im MYswl DB and then display them in a table on a web form
. also i am using Windows XP .tell me do i have to change things like "upload_temp_dir" and r"egister_global "setting in php.ini etc .kindly tell me right from the scratch.thanx


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sun Sep 11, 2005 12:52 pm
by josh
There is wonderful documentation on file uploads on the php.net site, that should cover getting the image on the filesystem, access the temp name in the $_FILES array, use file_get_contents() or any other binary safe method of reading the file's contents into a string, then use a mysql_query() to issue an insert statement, make sure you use mysql_real_escape_string() on the image string before you insert it, once you have it in the database here is one method on retreiving it:


file1.php

Code: Select all

$id = 3;
echo ("<img src=\"/file2.php?id={$id}\" />");

file2.php

Code: Select all

$id = (int)$_GET['id'];
// Read img string from DB

// Output image header

// Output image string