Jump to content

Recommended Posts

I have created a php form that allows a user to upload text and an image to a server. the text and the image url are stored in a mysql server. the aim is to retrieve the image from the server, according to the information stored in the database, and to display the image and the text on the website. all the text works, but the image won't show up-- only a small icon where the image should be. i know that images CAN show up on the website, because a direct img src link works just fine.

 

i'm going to post all three forms to make it easier to see how they connect. first, here is the html script:

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>post</title>
    </head>
    <body>
        POST<br/><br/>
        
<form enctype="multipart/form-data" action="posted.php" method="POST">
                title:<br />  <input type="text" name="title" size="93"/> (optional)<br />
                date: <br/> <input type="text" name="date" /> (JAN 10 09)<br/>
                words:<br />   <textarea rows="50" cols="80" name="post"></textarea><br />
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
picture: <input name="uploaded_file" type="file" /><br />
<input type="submit" value="POST DIT" />
</form>

        
    </body>
</html>

 

and here is the php script that uploads the information to the mysql database and the image to the server:

 

<?php


//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and its size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 350000)) {
    //Determine the path to which we want to save this file
      $newname = '/posts/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 350Kb are accepted for upload";
  }
} else {
echo "Error: No file uploaded";

}

mysql_connect("--", "--", "--", "--") or die(mysql_error()) ;
mysql_select_db("--") or die(mysql_error()) ;



$title = $_POST['title'];
$post = $_POST['post'];
$date = $_POST['date'];

$id= mysql_insert_id(); 

$size= ($_FILES["uploaded_file"]["size"]);


mysql_query("INSERT INTO Posts (Title,Content,Date, image_url, image_name, image_type, image_size)
VALUES ('$title', '$post', '$date', '$newname', '$filename', '$ext', '$size')" );

echo "$title $post $date";

?>

 

and here is the php form that SHOULD display the images and all the text:

 

<?php

$dbh = mysqli_connect("--", "--", "--", "--");


$sql = "SELECT Title,Content,Date FROM Posts ORDER BY id DESC";

$query = mysqli_query($dbh,$sql);



echo "<html>
<head></head><title>velcome</title>
<body>



if($query) {

$rows = mysqli_num_rows($query);

for($i = 0; $i < $rows; $i++) { 

$feed = mysqli_fetch_array($query); 

$title = $feed['Title'];
$post = $feed['Content'];
$date = $feed['Date'];
$pic = $feed['image_url'];

echo "<tr >\n";
echo "<td >\n";
echo "<a href=\'archive.php'><font face='Georgia, Times New Roman, Times, serif' color='#999999'>$title</font></a>\n"; 
echo "<p>$date</p><br/>";
echo " <p>$post</p><br/>\n";

echo "<img src='$pic'/>";
echo " <a href='delete.php'>delete this p0st</a>\n";
echo "<hr />\n";

echo "</td >\n";
echo "</tr>\n";

}
echo "</table>\n";


} 

echo"</body>
</html>"

?>

 

i have been struggling with this for a long time, thank you for your help!

Link to comment
https://forums.phpfreaks.com/topic/147402-solved-image-display/
Share on other sites

you're right-- i had taken out "img_src" from the request list... now the code reads like this:

 

$sql = "SELECT Title,Content,Date, image_url FROM Posts ORDER BY id DESC";

 

but it makes no difference! somehow, the images still show up as little icons instead of the real thing.

 

when i echo out "$pic", it prints the URL just fine. i tried changing the input form so that the url gets saved in lots of different ways:

 

/posts/ggallin.jpg

/home/content/e/l/v/elviapw/html/posts/ggallin.jpg

dirname/posts/ggallin.jpg

websitename.com/posts/ggallin.jpg

 

all of these urls print directly when i ask it to print $pic. so i know the image_url can be retrieved from the database, but for some reason the image itself just won't show up on the page. it will show up if type any of the above urls directly into the <img src> tag, but i want the images to appear within the posts automatically when they are uploaded.

 

any more thoughts?? i'm totally stumped!!

 

here's the portion of code under question one more time:

 

$dbh = mysqli_connect("--", "--", "--", "--");


$sql = "SELECT Title,Content,Date, image_url FROM Posts ORDER BY id DESC";

$query = mysqli_query($dbh,$sql);


if($query) {

$rows = mysqli_num_rows($query);

for($i = 0; $i < $rows; $i++) { 

$feed = mysqli_fetch_array($query); 

$title = $feed['Title'];
$post = $feed['Content'];
$date = $feed['Date'];
$pic = $feed['image_url'];
echo "<img src='$pic;' />";

Link to comment
https://forums.phpfreaks.com/topic/147402-solved-image-display/#findComment-773871
Share on other sites

it won't let me modify my above post, so i'm reposting below with the modified pary in bold (so sorry for the inconvenience)

you're right-- i had taken out "img_src" from the request list... now the code reads like this:

 

$sql = "SELECT Title,Content,Date, image_url FROM Posts ORDER BY id DESC";

 

but it makes no difference! somehow, the images still show up as little icons instead of the real thing.

 

when i echo out "$pic", it prints the URL just fine. i tried changing the input form so that the url gets saved in lots of different ways:

 

/posts/ggallin.jpg

/home/content/e/l/v/elviapw/html/posts/ggallin.jpg

dirname/posts/ggallin.jpg

websitename.com/posts/ggallin.jpg

 

all of these urls print directly when i ask it to print $pic. so i know the image_url can be retrieved from the database, but for some reason the image itself just won't show up on the page. it will show up if type the url /posts/ggallin.jp directly into the <img src> tag (only this url), but i want the images to appear within the posts automatically when they are uploaded.

 

any more thoughts?? i'm totally stumped!!

 

here's the portion of code under question one more time:

 

$dbh = mysqli_connect("--", "--", "--", "--");


$sql = "SELECT Title,Content,Date, image_url FROM Posts ORDER BY id DESC";

$query = mysqli_query($dbh,$sql);


if($query) {

$rows = mysqli_num_rows($query);

for($i = 0; $i < $rows; $i++) { 

$feed = mysqli_fetch_array($query); 

$title = $feed['Title'];
$post = $feed['Content'];
$date = $feed['Date'];
$pic = $feed['image_url'];
echo "<img src='$pic;' />";

Link to comment
https://forums.phpfreaks.com/topic/147402-solved-image-display/#findComment-773883
Share on other sites

YES! it works! the image displays. wonderful. thank you.

but suddenly i have encountered another problem, maybe not linked to the first. suddenly when i try to post a new picture i get this error message:

 

Warning: move_uploaded_file(/posts/lemonade.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/e/l/v/elviapw/html/posted.php on line 16

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpjEv1eY' to '/posts/lemonade.jpg' in /home/content/e/l/v/elviapw/html/posted.php on line 16

Error: A problem occurred during file upload!lemonade qww22 22ee

 

which is very strange, because the form worked fine before, and because i KNOW that the folder exists and contains images. HELP!

Link to comment
https://forums.phpfreaks.com/topic/147402-solved-image-display/#findComment-773912
Share on other sites

I thought move_uploaded_file used filename, new location (not the filename, just the location directory where you want it to go). You appear to be sending the original file location correctly, but the second part of the move_uploaded_file command seems to have the full name of the file i.e. 'posts/lemonade.jpg' instead of just 'posts/'

 

Then again it's 6:12am and I just want to go to bed, so blame lack of sleep if I make no sence :)

Link to comment
https://forums.phpfreaks.com/topic/147402-solved-image-display/#findComment-773964
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.