SitePoint Sponsor |
|
User Tag List
Results 1 to 13 of 13
Thread: echoing a stored variable
Hybrid View
-
May 8, 2009, 23:26 #1
- Join Date
- Aug 2007
- Location
- Earth
- Posts
- 1,766
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
echoing a stored variable
hey all.i m trying to echo back a picture i uploaded with a script but i m failing to do that. am i missing something or doing something wrong here is the code
Code PHP:<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); //echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?>
-
May 8, 2009, 23:46 #2
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
With file_exists and move_uploaded_file, the path needs to be the full path to the file / directory.
PHP Code:if (file_exists("/put/your/full/path/to/upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/put/your/full/path/to/upload/" . $_FILES["file"]["name"]);
//echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
-
May 8, 2009, 23:48 #3
-
May 9, 2009, 03:20 #4
- Join Date
- Aug 2007
- Location
- Earth
- Posts
- 1,766
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
hey folks i m echoing a stored image in database. which when tried showed me boxes and stuff not the image. here is my code
Code PHP:<?php /*?>Database conection<?php */?> <?php $con = mysql_connect(hidden); if(!$con){ echo "cannot connect to the database" .mysql_error(); } $grab = mysql_select_db("foodpak"); if(!$grab){ echo "cannot select the database" .mysql_error(); } ?> <?php $result = mysql_query("SELECT * FROM description"); while ($row= mysql_fetch_array($result)) { echo $row['image']; } ?>
-
May 9, 2009, 03:41 #5
- Join Date
- May 2009
- Posts
- 14
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:header('Content-Type: image/gif');
-
May 9, 2009, 03:45 #6
-
May 9, 2009, 03:52 #7
- Join Date
- May 2009
- Posts
- 14
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:<?php
//
//Database Connection
//
$con = mysql_connect(hidden);
if(!$con){
echo "cannot connect to the database" .mysql_error();
}
$grab = mysql_select_db("foodpak");
if(!$grab){
echo "cannot select the database" .mysql_error();
}
$result = mysql_query("SELECT * FROM description");
//since you can only output one image from blob at a time
if($row = mysql_fetch_array($result))
{
header('Content-type: '.$row['image_type']);
echo $row['image'];
}
?>
-
May 10, 2009, 05:42 #8
- Join Date
- Aug 2007
- Location
- Earth
- Posts
- 1,766
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
hey folks i m trying to display my images from DB on webpage. i m successful for getting only one image to show up where as second image is in my db but it isnt showing here is the code i m using to echo stored images in database
Code PHP:<?php header('Content-Type: image/gif'); $con = mysql_connect(suprresed); if(!$con){ echo "Error connecting" .mysql_error(); } $grab = mysql_select_db("foodpak"); if(!$grab){ echo "Error selecting database" .mysql_error(); } $result = mysql_query("SELECT * FROM Listing"); while ($row=mysql_fetch_array($result)) { echo $row[image]."<br/>"; } ?>
-
May 10, 2009, 10:35 #9
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
Each file can only output one image. You would need to run the query separately from reading the image contents.
PHP Code:$con = mysql_connect(suprresed);
if(!$con){
echo "Error connecting" .mysql_error();
}
$grab = mysql_select_db("foodpak");
if(!$grab){
echo "Error selecting database" .mysql_error();
}
$result = mysql_query("SELECT * FROM Listing");
while ($row=mysql_fetch_array($result))
{
echo '<img src="/fetch_image.php?name='$row[image].'" /><br/>';
}
PHP Code:$errorImage = 'image_not_exists.gif';
$dir = 'path/to/image/directory';
$im = isset($_GET['name'])?$_GET['name']:$errorImage;
$path = file_exists($dir.'/'.$file)?$dir.'/'.$name:$dir.'/'.$errorImage;
header('Content-Type: image/gif');
// read and output image file
-
May 10, 2009, 10:58 #10
- Join Date
- Aug 2007
- Location
- Earth
- Posts
- 1,766
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
thnx, do u any script or can find anything that can make a form in which i haver few text field and option to upload image and it can be shown on a webpage? i m creating a project and this image thing is driving me nuts coz as if i submit the form i created into mysql. the image field which is blob.remains size 0.maybe i m sending it directly into mysql instead of storing it in a folder coz i donno how to do much of image store and retrieve in mysql and i have deadline to meet. or if any1 here can make me a small simple form which upload text fields and a image into DB and retrieve it back on a given page
-
May 10, 2009, 11:50 #11
- Join Date
- May 2009
- Posts
- 14
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
My 2 cents...
You don't want to store images in a db, you want them stored as actual files. You should use the db to store the location of the images but not the actual images. Storing them in a db complicates everything and makes it really slow.
-
May 10, 2009, 11:58 #12
- Join Date
- Jul 2006
- Location
- Augusta, Georgia, United States
- Posts
- 4,194
- Mentioned
- 17 Post(s)
- Tagged
- 5 Thread(s)
Yeah, you shouldn't store images inside the db. If you need them to be protected then store the actual files outside the site root and only grant privileges based on session data perhaps. Then again I'm not certain of the requirements of your system so that would only be a speculation based on what has been said here.
-
May 10, 2009, 12:26 #13
Bookmarks