Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用新名称将文件移动到新位置_Php - Fatal编程技术网

Php 使用新名称将文件移动到新位置

Php 使用新名称将文件移动到新位置,php,Php,我正在尝试将现有图像文件从临时文件夹移动到具有适当文件名的适当位置,但由于某些原因,它总是失败 function move_temp_image($article_id) { global $db, $config; if (($image_load = !@fopen($_SERVER['DOCUMENT_ROOT'] . "/uploads/temp/{$_SESSION['username']}_article_tagline.jpg", 'r+')) &&

我正在尝试将现有图像文件从临时文件夹移动到具有适当文件名的适当位置,但由于某些原因,它总是失败

function move_temp_image($article_id)
{
    global $db, $config;

    if (($image_load = !@fopen($_SERVER['DOCUMENT_ROOT'] . "/uploads/temp/{$_SESSION['username']}_article_tagline.jpg", 'r+')) && ($image_load = !@fopen($_SERVER['DOCUMENT_ROOT'] . "/uploads/temp/{$_SESSION['username']}_article_tagline.png", 'r+')) && ($image_load = !@fopen($_SERVER['DOCUMENT_ROOT'] . "/uploads/temp/{$_SESSION['username']}_article_tagline.gif", 'r+')))
    {
        $this->error_message = "Could not find temp image to load?";
        return false;
    }

    else
    {
        $image_info = getimagesize($image_load);
        $image_type = $image_info[2];
        $file_ext = '';
        if( $image_type == IMAGETYPE_JPEG ) 
        {
            $file_ext = 'jpg';
        } 

        else if( $image_type == IMAGETYPE_GIF )
        {
            $file_ext = 'gif';
        } 

        else if( $image_type == IMAGETYPE_PNG )
        {
            $file_ext = 'png';
        }

        // give the image a random file name
        $imagename = rand() . 'id' . $article_id . 'gol.' . $file_ext;

        // the actual image
        $source = $image_load;

        // where to upload to
        $target = $_SERVER['DOCUMENT_ROOT'] . "/uploads/articles/topimages/" . $imagename;  

        if (rename($source, $target))
        {           
            // remove old temp image
            if ($image['article_top_image'] == 1)
            {
                unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/temp/' . $image_load);
            }

            unset($_SESSION['temp_tagline']);

            $db->sqlquery("UPDATE `articles` SET `article_top_image` = 1, `article_top_image_filename` = ? WHERE `article_id` = ?", array($imagename, $article_id));
            return true;
        }

        else
        {
            $this->error_message = 'Could not move temp file to tagline uploads folder!';
            return false;
        }
    }
}

我不确定我做错了什么,我认为重命名是实现这一点的方法,但我显然忽略了一些东西。

您在
$source
中没有文件名-在您的代码中,
$source
包含一个文件资源

重命名不仅仅适用于文件名,因此您必须更改代码

i、 e.用以下代码替换您的状况

$types = array('jpg', 'png', 'gif');
$file = $_SERVER['DOCUMENT_ROOT'] . "/uploads/temp/{$_SESSION['username']}_article_tagline.";
$image_load = false;
foreach ($types as $type) {
    if (file_exists($file . $type)) {
        $image_load = $file . $type;
        break;
    }
}

if (!file_exists($image_load))

您是否检查了目标目录的权限?允许您的Web服务器用户对其进行写入吗?是的,权限是可以接受的,因为我一直将其上载到其他位置。您是否收到任何警告?是否达到最大值?这需要进行基本调试。什么时候会失败?
$source
包含什么,
$target
包含什么?它可能是一个.jpg、.png或.gif文件,因此无法工作,这就是我在开始时进行三重检查的原因。不过,您回答的第一行是问题所在,我没有意识到使用带字符串的fopen也不会将其设置为文件名。