将图像上传到现有文件夹PHP

时间:2013-02-13 18:59:36

标签: php upload

<?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";
?>

我正在处理的代码有效但我必须为我的管理文件夹创建一个新的“图像”文件夹。有没有办法可以将文件上传到admin文件夹之外并将其移动到原始的“image”文件夹“。我知道这很混乱,但我的目录看起来像这样。

clubmaru -admin  -images

-CSS -图片 -js

2 个答案:

答案 0 :(得分:1)

您可能正在寻找PHP的 重命名 功能。 http://php.net/manual/en/function.rename.php

将oldname参数设置为文件(带有路径),将newname参数设置为您想要的位置(显然还有新路径)

确保要将文件移动到的“图像文件夹”具有正确的权限集,确保其可写。您还可以考虑更改 move_uploaded_file 中的参数,以便将文件放在首位!

答案 1 :(得分:0)

是的,有一种方法,你需要改变路径。现在你的路径为images/$name,这意味着它会将文件放在本地目录中的images目录中,并运行到正在运行的脚本中。

使用目录布局:

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

你使路径相对(或找到另一种选择)

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

这意味着,上一个目录,进入css然后进入图像。