Jump to content

Can anyone help with code for uploading a picture from MySQL?


sfaisal

Recommended Posts

Hello, Can anyone help me with my code, I am trying to retrieve a picture that I saved in a file on my server, the name was saved in MySQL and I want to retrieve it after it is uploaded and post it in a comment box.  Here is the code:  The problem I am having is te picture uploads perfect, but then it gives me a filename not found error when it is suppose to display the picture.  Please help!!!!!!!

 

<?php
include('config3.php'); 
 
if($_POST)
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
 
if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading 
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
 
}
else
{
// check if file already exit in "images" folder.
if (file_exists("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{  
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$query_image = "INSERT into acc_images (image, status, acc_id) values ('".$_FILES['file']['name']."', 'display','')";
if(mysql_query($query_image))
{
}
}
}
 
 
}
 
$query_image = "SELECT * FROM acc_images acc_ id='$id'";
 
 $result = mysql_query($query_image);
 if(mysql_num_rows($result) > 0)
 {
   while($row = mysql_fetch_array($result))
   {
  echo '<img alt="" src="uploads/'.$row["image"].'">';
   }
 }
  echo 'File name not found in database';
 }
?>
<html>
<body>
<form action="upload.php" method="post"enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>
 
</body>
</html>
Link to comment
Share on other sites

Please use code tags and format your code properly.

 

<?php
include('config3.php');

if ($_POST) {
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.

    if ($_FILES["file"]["error"] > 0) {
// if there is error in file uploading 
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

    } else {
// check if file already exit in "images" folder.
        if (file_exists("uploads/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
            if (move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"])) {
// If file has uploaded successfully, store its name in data base
                $query_image = "INSERT into acc_images (image, status, acc_id) values ('" . $_FILES['file']['name'] . "', 'display','')";
                if (mysql_query($query_image)) {
                }
            }
        }


    }

    $query_image = "SELECT * FROM acc_images acc_ id='$id'";

    $result = mysql_query($query_image);
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_array($result)) {
            echo '<img alt="" src="uploads/' . $row["image"] . '">';
        }
    }
    echo 'File name not found in database';
}
?>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"/>
    <br/>
    <input type="submit" name="submit" value="Submit"/>
</form>

</body>
</html>
Your code doesn't work because you're not actually storing the file in the file system. Once a file is uploaded in PHP, it is stored in a temporary location until the script is done executing, where it is then deleted. You need to copy the file to a permanent location after it is uploaded.

 

Take a look at move_uploaded_file()

Edited by scootstah
Link to comment
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.