Forum Moderators: coopster

Message Too Old, No Replies

Php code for image upload and display

Php code for file image upload and display

         

chacko

5:24 am on Feb 21, 2006 (gmt 0)

10+ Year Member



Hi All,

Can any one give me code for logging in, image upload to a particual folder, add text and log out. Once I log out the image and text should display on my browser. I know most of the process, but the problem is i can't seem to get the file upload to a particular folder.

thanks

omoutop

7:00 am on Feb 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the simliest script i have for image upload :

$serverpath = "../hotel_images";// Path to where images should be uploaded to on the server.
$urltoimages = "../hotel_images"; // Web address to where the images are accessible from.

$maxsize = "100000";
$file = $_FILES['file']['name'];
$allowedfiles[] = "jpg";
$allowedfiles[] = "JPG";

if($_FILES['file']['size'] > $maxsize)
{
$msg= "File size too large. Reduce and <a href=\"".$_SERVER['PHP_SELF']."?id=".$id."\">try again</a>.";
}
else
{
$path = "$serverpath/$file";
foreach($allowedfiles as $allowedfile)
{
if (file_exists($path))
{
$msg= "File name exists - rename and <a href=\"".$_SERVER['PHP_SELF']."?id=".$id."\">try again</a>.";
$done = "yes";
break;
}

if (substr($file, -3) == $allowedfile)
{
move_uploaded_file($_FILES['file']['tmp_name'], "$path");
$done = "yes";
$msg="";
}
// insert image name to db (just in case)
$sql11 = "UPDATE image_table SET image_thumb='$file' WHERE id='$id' ";
$result11 = mysql_query($sql11) or die(mysql_error().'<p>'.$sql11.'</p>');
}

if ($done <> "yes")
{
$msg= "<p><b>Error:</b> Your image has not been uploaded becuase it is not a recognised image file. Please try again.</p>";
}
}

chacko

12:23 pm on Feb 22, 2006 (gmt 0)

10+ Year Member



I had created one database in mysql & by writing following code I tried to upload & download it but it just not uploads the file in my database.What should I do to find the problem?
Databse-

CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);
upload form-

<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

php file-

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";
}
?>

omoutop

2:03 pm on Feb 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



dont try to save the file in db, save only the filename.

the file must be saved in specific folder with read/write access