设置图像上传路径

时间:2015-05-18 05:34:04

标签: php file-upload

我正在尝试将图片上传脚本添加到我的网站,我认为我没有正确配置它来访问我的目录。

我要上传到的目录:images/gallery/

我的代码所在的目录:scripts/php/imageUpload.php

imageUpload.php中的php代码:

<?php
$output_dir = "../images/gallery/";

if(isset($_FILES["myfile"]))
{
    $ret = array();

    $error =$_FILES["myfile"]["error"];
   {

        if(!is_array($_FILES["myfile"]['name'])) //single file
        {
            $RandomNum   = time();

            $ImageName      = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
            $ImageType      = $_FILES['myfile']['type']; //"image/png", image/jpeg etc.

            $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
            $ImageExt       = str_replace('.','',$ImageExt);
            $ImageName      = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
            $NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;

            move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $NewImageName);
             //echo "<br> Error: ".$_FILES["myfile"]["error"];

                 $ret[$fileName]= $output_dir.$NewImageName;
        }
        else
        {
            $fileCount = count($_FILES["myfile"]['name']);
            for($i=0; $i < $fileCount; $i++)
            {
                $RandomNum   = time();

                $ImageName      = str_replace(' ','-',strtolower($_FILES['myfile']['name'][$i]));
                $ImageType      = $_FILES['myfile']['type'][$i]; //"image/png", image/jpeg etc.

                $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
                $ImageExt       = str_replace('.','',$ImageExt);
                $ImageName      = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
                $NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;

                $ret[$NewImageName]= $output_dir.$NewImageName;
                move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$NewImageName );
            }
        }
    }
    echo json_encode($ret);

}

?>

我尝试将目录从../images/gallery/更改为/images/gallery/并更改为images/gallery/

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果你在Windows操作系统上使用wamp试试这个

$output_dir = $_SERVER['DOCUMENT_ROOT'] . "images/gallery/";

否则试试这个

$output_dir = dirname(__FILE__)."/../images/gallery/";