0

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


?>
2
  • move_uploaded_files is only for files that were uploaded via POST. The source filename should come from $_FILES['xxx']['tmp_name']. Commented Jul 31, 2013 at 18:20
  • hey, so file was uploaded by post to the server temp file. I then moved it once to my own temp folder(not server temp folder), so i can view it. and now i want to move from mytemp folder to another folder, my permant upload folder. Commented Jul 31, 2013 at 18:24

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

k, i'm test it now to see if it works. Didn't know that. Just assumed move_uploaded_files was for moving all files. Thanks.
Always check the manual: "This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism)."
k thank Barmar, you were right. I was just missing and .'.'. before $extension

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.