File upload from form to tmp directory?


#1

Hi,

So I’ve got an HTML form where users can open a file browser and upload files. $_FILES[‘files’][‘tmp_name’] for an individual file in PHP comes out something like this: /tmp/phpJspGSJ

They’re image files, so I’m trying to load them into ImageMagick to do stuff to them, but it’s not finding the file. This is a clearer error message, actually through gd2’s getimagesize(), but either method finds no file:

Warning: getimagesize(/tmp/phpJspGSJ) [function.getimagesize]: failed to open stream: No such file or directory in /home/[snip]

So my question is, where am I looking for this tmp file? sys_get_tmp_dir says the tmp directory is just “/tmp/”

enctype in the form is set to “multipart/form-data,” BTW. I know the previous question like this one mentioned that as a possible solution, so.

edit: Here’s code. Note, I’m ajaxing my info around. From profile.php, after submitting the form:

[php]if ((empty($_POST) === false) && ($_SESSION[‘perms’] == 1)) {
$_POST[‘id’] = $_SESSION[‘id’];
$_FILES[‘files’][‘filename’] = $_POST[‘filename’];
$_FILES[‘files’][‘fileyear’] = $_POST[‘fileyear’];
unset($_POST[‘filename’]);
unset($_POST[‘fileyear’]);
$_SESSION[‘files’] = $_FILES;
if (isset($values[0])) {
$profile->create_profile($_POST, ‘update’);
} else {
$profile->create_profile($_POST, ‘new’);
}
}

if (isset($_SESSION[‘files’]) && ($_SESSION[‘perms’] == 1)) { ?>

<?php
}
?>[/php]

The ajax pass:

[code]$(document).ready(function() {
$("#image-status").html(‘Processing images…’);
});

function process_images(files) {
$.ajax ({
type: “POST”,
url: “core/imageprocessor.php”,
data: {files: JSON.stringify(files)},
success: function(msg){
$("#image-status").html(msg);
}
});
}[/code]

imageprocessor.php:

[php]<?php
session_start();
require_once("…/includes/db_connect.php");
if (isset($_POST)) {
$files = json_decode($_POST[‘files’], true);
$files = $files[‘files’];
unset($files[‘files’]);
echo process_pics($files);
} else {
echo “NOPE”;
}

function process_pics($files) {
$tmp_file = $files[‘tmp_name’][0];
$im = new Imagick();
//$im->pingImage($tmp_file);
//$im->readImage($tmp_file);
$size = getimagesize($tmp_file);
return $size;
}[/php]

Also, weird, replacing the getimagesize() with this:

[php]
$filename = DOC_ROOT . ‘/epic/tmp/’ . $files[‘name’][0];
move_uploaded_file($tmpFile, $filename);
[/php]

doesn’t move the file to that place, but also doesn’t output an error. ($files[‘name’][0] being the original file name)


#2

The problem was the AJAXing. I’ve set it to move the temporary file into the proper file name in a directory of my creation before the AJAX call, and that works.