views:

414

answers:

2

I'm trying to use Uploadify (a jQuery plugin) with my CakePHP app. Locally (WampServer), it works great, but when I try it on my live server (Dreamhost), the files don't show up. I've properly chmod'ed the folders, checked the paths, etc, and I can't make any sense of why it isn't working. Here's upload.php:

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . "/app/webroot/posts/temp/";
    $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

    $targetFile = str_replace(".", "_" . mt_rand(10000000,99999999) . ".", $targetFile);

    move_uploaded_file($tempFile,$targetFile);
}
echo "1";

This script is definitely being run, but I've looked in the specified folder (and all over the filesystem), and the uploaded file(s) just aren't showing up! It's driving me crazy--hopefully someone has the answer to this. Please let me know if I should post any more code, and I will.

+1  A: 

You could change $targetPath so it is relative to the document rather than the exact server path. I had a similar problem a while ago.

Ben Shelock
+2  A: 

Turns out, it was this line:

$targetFile = str_replace(".", "_" . mt_rand(10000000,99999999) . ".", $targetFile);

This line was meant to append a random series of numbers to the filename to avoid collisions. However, as you can see, it's operating on the whole path, not just the filename. Well, my domain name is in my path (i.e. mydomain.com), and thus was getting changes to mydomain_12314123402.com, which obviously is a path that doesn't exist.

Man, I feel like an idiot!

mgroves
Ya, you should use pathinfo to get the appropriate path part to operate on.
Justin Johnson
That, or just move my random number append to the filename before I construct the full path, which is what I did.
mgroves