Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying pictures from path in PHP from MySQL
    primarykey
    data
    text
    <p>I am in the early stage of developing a bidding system whereby users are able to insert items that they want to sell. Here is the script to copy the image from the temporary path and store the path of the image to another folder.</p> <pre><code> define ("MAX_SIZE","10000"); //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; }; //This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded. $errors = 0; //Checks if the form has been submitted if (isset($_POST['Submit'])) { //Reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //If it is not empty if ($image) { //Get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //Get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //If it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests if (($extension != "jpg") &amp;&amp; ($extension != "jpeg") &amp;&amp; ($extension != "png") &amp;&amp; ($extension != "gif")) { //Print error message echo '&lt;h1&gt;Unknown extension!&lt;/h1&gt;'; $errors=1; } else { //Get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //Compare the size with the maxim size we defined and print error if bigger if ($size &gt; MAX_SIZE*111111111111024) { echo '&lt;h1&gt;You have exceeded the size limit!&lt;/h1&gt;'; $errors=1; } //We will give an unique name, for example the time in Unix time format $image_name=time().'.'.$extension; //The new name will be containing the full path where will be stored (images folder). $imagepath='C:\\xampp\\htdocs\\biddingsystem\\Images\\' . $image_name; //We verify if the image has been uploaded, and print an error instead $copied = copy($_FILES['image']['tmp_name'], $imagepath); if (!$copied) { echo '&lt;h1&gt;Picture upload failed!&lt;/h1&gt;'; $errors=1; } } } } //If no errors registred, print the success message if(isset($_POST['Submit']) &amp;&amp; !$errors &amp;&amp; isset($_POST['image'])) { echo "&lt;h1&gt;Picture Uploaded Successfully! Try again!&lt;/h1&gt;"; } </code></pre> <p>Then, I inserted the image path along with other data to a MySQL database with the script below and tried to display them back to the user using a table. Other data worked out fine but for the image display (imagepath), only the path of the image gets displayed, not the image itself.</p> <pre><code>mysql_query("INSERT INTO items (username, item, price, description, start_date, start_time, imagepath) VALUES ('$username', '$_POST[item]', '$_POST[price]', '$_POST[description]','$_POST[start_date]', '$_POST[start_time]', '$imagepath') ") or die ("Error - Couldn't add item"); echo "Item added successfully"; echo "&lt;h1&gt;You have added the following item:&lt;/h1&gt;"; $sql = "SELECT item, price, description, start_time, start_date, imagepath FROM items WHERE username = '$username' AND item='$_POST[item]'"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo"&lt;table border=2&gt; &lt;tr&gt;&lt;td&gt;Item&lt;/td&gt; &lt;td&gt;Price&lt;/td&gt;&lt;td&gt;Description&lt;/td&gt;&lt;td&gt;Start time&lt;/td&gt;&lt;td&gt;Start date&lt;/td&gt;&lt;td&gt;Picture&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt; $row[item]&lt;/td&gt; &lt;td&gt; $row[price]&lt;/td&gt; &lt;td&gt; $row[description]&lt;/td&gt; &lt;td&gt; $row[start_time]&lt;/td&gt; &lt;td&gt; $row[start_date]&lt;/td&gt; &lt;td&gt; $row[imagepath]&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;&lt;/br&gt;"; </code></pre> <p>I have tried using <code>&lt;img src="&lt;?php $imagepath ?&gt;"&gt;</code> to show the image but to no avail. I have even tried storing the actual image itself in the database using the BLOB type. However, the result is a page filled with strange characters. How do I fix this problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload