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

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

clubmaru -admin -images

-css -图像 -js

4

2 回答 2

1

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

将 oldname 参数设置为文件(及其路径),将 newname 参数设置为您想要的位置(显然,与新路径一起)

只需确保要将文件移动到的“图像文件夹”具有正确的权限设置,确保它是可写的。您可能还需要考虑更改move_uploaded_file中的参数,以首先将文件放在您想要的位置!

于 2013-02-13T19:09:32.760 回答
0

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

使用目录布局:

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

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

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

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

于 2013-02-13T19:10:03.370 回答