seamus
seamus

Reputation: 2901

php move_uploaded_file not working

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.

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.

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.

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

--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();


?>

Upvotes: 0

Views: 792

Answers (1)

Barmar
Barmar

Reputation: 780994

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.

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

Upvotes: 6

Related Questions