PHP的move_uploaded_file不起作用 [英] php move_uploaded_file not working

查看:60
本文介绍了PHP的move_uploaded_file不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我会为此简化代码,或者只显示给我带来麻烦的部分.但是在这种情况下,我不知道出了什么问题,所以我将整个内容粘贴到了.抱歉.

Usually i'd simplify my code for this, or only show the part thats giving me trouble. But in this case I have NO idea what is going wrong so I pasted the entire thing in. Sorry.

好,因此以下脚本获取ajax发送的值,并将其上载到sql数据库中.完成此操作后,它将图像从一个文件夹移动到另一个文件夹.

Ok so the below script gets values sent by ajax and uploads them into an sql data base. After doing that it moves an image from a folder into another folder.

除了"move_uploaded_file"位以外,整个脚本可以正常工作,可以完成其应做的工作.因此它正确执行了sql部分,并且所有会话名称,字符串编辑等都正确.

The entire script works fine, does what its supposed to do, except for the 'move_uploaded_file' bit. So it does the sql part correctly, and all session names, string edits, etc... are correct.

我已经回显了脚本生成的文件名,它们是正确的.文件夹也可以读写.文件夹中存在等待移动的文件.

I've echoed the names of the files the script produces and they are correct. The folders can be read+written too. And the file waiting to be moved is present in the folder.

我想念什么?为什么move_uploaded_file不起作用?预先谢谢大家.

What am I missing? Why is move_uploaded_file not working? Thanks in advance all.

-将move_uploaded_file()更改为named(),但仍然无法正常工作-

--changed move_uploaded_file() to rename(), still not working--

<?php 

session_start();
unset($_SESSION['reference']);

$name = $_GET['name'];
$category = $_GET['category'];
$subCategory = $_GET['subCategory'];
$date = $_GET['date'];
$address = $_GET['address'];
$city = $_GET['city'];
$state = $_GET['state'];
$host = $_GET['host'];
$info = $_GET['info'];
$adder = $_SESSION['user'];

//turn into array
$array = array();
$array[0]=$name;
$array[1]=$category;
$array[2]=$subCategory;
$array[3]=$date;
$array[4]=$address;
$array[5]=$city;
$array[6]=$state;
$array[7]=$host;
$array[9]=$info;
$array[11]=$adder;


try {

$con = new PDO('mysql:host=localhost;dbname=test');
    //$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $refid=$con->prepare(" SELECT MAX(id) FROM createX "); 
    $refid->execute();
    $id = $refid->fetchColumn();
    $id=$id+1;
    $newDate = str_replace('-', '', $date); 
    $reference = $id.$newDate;
    $array[10]=$reference;
    $array[8] = $_SESSION['imagePath'].$reference.'.'.$_SESSION['imageExt'];

    $insert = $con->prepare(" INSERT INTO createX 
    (name,category,subCategory,date,address,city,state,host,imagePath,info,ref,adder)
        VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ");
    $insert->execute($array);   

    rename( '../tempUploads/'.$_SESSION['imagePath'].$_SESSION['imageExt'] ,
    '../uploads/'.$_SESSION['imagePath'].$reference.'.'.$_SESSION['imageExt'] ); 

}

catch(PDOException $e) { //try
    echo 'error';
//echo 'ERROR: ' . $e->getMessage();
}

$_SESSION['reference'] = $reference;

unset($array);
session_write_close();


?>

推荐答案

move_uploaded_file() 仅适用于通过POST上传的文件.源文件名应来自 $ _ FILES ['xxx'] ['tmp_name'] ,并相对于用于临时保存已发布文件的目录进行解释.

move_uploaded_file() is only for files that were uploaded via POST. The source filename should come from $_FILES['xxx']['tmp_name'], and is interpreted relative to the directory used to temporarily hold posted files.

如果要移动其他文件,请使用 rename() .

If you want to move some other file, use rename().

这篇关于PHP的move_uploaded_file不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆