Eunice Delfin
Eunice Delfin

Reputation: 11

Upload image to existing folder PHP

<?php
include('includes/db.php');

$drinks_cat = $_POST['drinks_cat'];
$drinks_name = $_POST['drinks_name'];
$drinks_shot = $_POST['drinks_shot'];
$drinks_bottle = $_POST['drinks_bottle'];
$drinks_availability = 'AVAILABLE';

$msg = "ERROR: ";
$itemimageload="true";
$itemimage_size=$_FILES['image']['size'];
$iname = $_FILES['image']['name'];
if ($_FILES['image']['size']>250000){$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$itemimageload="false";}

if (!($_FILES['image']['type'] =="image/jpeg" OR $_FILES['image']['type'] =="image/gif" OR $_FILES['image']['type'] =="image/png"))
{$msg=$msg."Your uploaded file must be of JPG , PNG or GIF. Other file types are not allowed<BR>";
$itemimageload="false";}

$file_name=$_FILES['image']['name'];
$add="images"; // the path with the file name where the file will be stored


if($itemimageload=="true")
{
    if (file_exists($add) && is_writable($add)) 
    {
        if(move_uploaded_file ($_FILES['image']['tmp_name'], $add."/".$_FILES['image']['name']))
        {
        echo "Image successfully updated!";
        }
        else
        {
        echo "Failed to upload file Contact Site admin to fix the problem";
        }
    }
    else 
    {
    echo 'Upload directory is not writable, or does not exist.';
    }
}
else
{
    echo $msg;
}

$dir = $add."/".$iname;
echo "<BR>";
// Connects to your Database



mysql_query("INSERT INTO `product_drinks`(`drinks_id`, `drinks_cat`, `drinks_name`, `drinks_shot`, `drinks_bottle`, `drinks_image`, `drinks_availability`) VALUES (NULL,'".$drinks_cat."', '".$drinks_name."','".$drinks_shot."','".$drinks_bottle."','".$dir."','".$drinks_availability."')") or die("insert error");

Print "Your table has been populated";
?>

The code I'm working on works but i have to create a new "image" folder for my admin folder. Is there any way that I could upload the file outside the admin folder and move it to to the original "image" folder". I know it's quite confusing but my directory looks like this.

clubmaru -admin -images

-css -images -js

Upvotes: 0

Views: 2172

Answers (2)

UnholyRanger
UnholyRanger

Reputation: 1971

Yes there is a way, you need to change the path. Right now you have the path as images/$name which means that it will put the file in the images directory found in the local directory to the script that is running.

Using directory layout:

clubmaru
  ->admin
    ->script.php (the upload file)
    ->images
  ->css
    ->images
      ->js

You make the path relative (or find another alternative)

$add="../css/images";

This means, go up a directory, go into css then into images.

Upvotes: 0

Sites Done Right
Sites Done Right

Reputation: 652

You may be looking for PHP's rename function. http://php.net/manual/en/function.rename.php

Set the oldname parameter to the file (with its path) and the newname parameter to where you want it to be (along with the new path, obviously)

Just ensure the "image folder" you want to move the file to has the correct permissions set ensure it's writable. You also may want to consider changing the parameter in your move_uploaded_file to put the file where you want it in the first place!

Upvotes: 1

Related Questions