0

In this php code I want to customize the image upload destination. with this php file, I have directory called uploads. I want to add all my uploaded images to this directory and store path in db. how can I do this?

<?php
// Assigning value about your server to variables for database connection
$hostname_connect= "localhost";
$database_connect= "image_upload";
$username_connect= "root";
$password_connect= "";

$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR); 
@mysql_select_db($database_connect) or die (mysql_error()); 

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("images/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {  
            //move_uploaded_file function will upload your image.  if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
            if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_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)) {
                    echo "Stored in: " . "images/" . $_FILES["file"]["name"];
                } else {
                    echo 'File name not stored in database';
            }
       }
   }    
}
}
?>

currently when I run the upload I am getting warnings

Warning: move_uploaded_file(images/1409261668002.png): failed to open stream: No such file or directory in D:\xampp\htdocs\image-upload\index.php on line 29

Warning: move_uploaded_file(): Unable to move 'D:\xampp\tmp\php1C1F.tmp' to 'images/1409261668002.png' in D:\xampp\htdocs\image-upload\index.php on line 29

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
CJAY
  • 6,989
  • 18
  • 64
  • 106
  • Where does your project live i.e. is it in `D:\xampp\htdocs` or `D:\xampp\htdocs\project_folder` – RiggsFolly Aug 04 '15 at 12:00
  • @RiggsFolly in xampp\htdocs\project_folder – CJAY Aug 04 '15 at 12:01
  • 2
    Then its fairly obvious. The path in your code of `"images/" . $_FILES["file"]["name"]` will equate to `D:\xampp\htdocs\images` and I bet you have created `D:\xampp\htdocs\project\images`. Its a DocumentRoot thing. To get sites to run correctly, you are well advised to create a Virtual Host for each of your projects even if they live in `D:\xampp\htdocs\project1`, `D:\xampp\htdocs\project2`. Then you get a correct `DocumentRoot` and all the folder based code works as expeted – RiggsFolly Aug 04 '15 at 12:10
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 04 '15 at 12:18

2 Answers2

0

You must specify a correct path, the 'images/1409261668002.png' path doesn't exist if you dont create them and don't specify them.

if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]))) { .... }

You must specify the absolute path

wilson
  • 334
  • 1
  • 15
  • 1
    Probably not that looks like a Windows folder name in the move error – RiggsFolly Aug 04 '15 at 11:59
  • Oh yeah, i just didn't look at the path, but in this error the path `Unable to move 'D:\xampp\tmp\php1C1F.tmp' to 'images/1409261668002.png' in D:\xampp\htdocs\image-upload\index.php` don't exist, i edit my answer – wilson Aug 04 '15 at 12:02
0

You can use below code:

$image=basename($_FILES['file']['name']);
$image=str_replace(' ','|',$image);

$tmppath="images/".$image;
if(move_uploaded_file($_FILES['file']['tmp_name'],$tmppath))
{...}

Let me know if you have any query/concern regarding this.

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • 1
    Why should the OP try this? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Aug 04 '15 at 12:19