【问题标题】:Upload image to existing folder PHP将图像上传到现有文件夹 PHP
【发布时间】:2013-02-13 18:59:36
【问题描述】:
<?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 -行政 -图片

-css -图片 -js

【问题讨论】:

  • 你可以把它移到任何你喜欢的地方,我不明白这个问题
  • 只需使用 $add="../images";或使用完整的真实路径,如 (C:\user\images) 或在 linux /home/user/public_html/images
  • 这个脚本很危险。它充满了安全漏洞,以至于将其放在生产系统上将使服务器完全受到远程攻击。不要使用此代码。

标签: php upload


【解决方案1】:

您可能正在寻找 PHP 的 rename 函数。 http://php.net/manual/en/function.rename.php

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

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

【讨论】:

    【解决方案2】:

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

    使用目录布局:

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

    您使路径相对(或找到其他替代方法)

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2014-02-25
      • 2018-05-28
      相关资源
      最近更新 更多