sfaisal Posted July 2, 2015 Share Posted July 2, 2015 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> Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 2, 2015 Share Posted July 2, 2015 (edited) 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 July 2, 2015 by scootstah Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 2, 2015 Share Posted July 2, 2015 You also have an error in your sql syntax. $query_image = "SELECT * FROM acc_images acc_ id='$id'"; Should be: $query_image = "SELECT * FROM acc_images WHERE acc_ id='$id'"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.