Jump to content

Adding variable to File Upload Path


sh0wtym3

Recommended Posts

I am working on an upload script, and I need the uploads to be uploaded to a different folder depending on which user is logged in. Here's my code:

session_start();

$alias = $_SESSION['alias'];

$filepath = getcwd()."/uploads/$alias/";

if(!file_exists($filepath)) {
mkdir($filepath,0777);
}
chmod($filepath,0777);

if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $filepath.$_FILES['Filedata']['name'])){
chmod($filepath.$_FILES['Filedata']['name'], 0777);
}

 

But it does not work? However when I rewrite this line:

$filepath = getcwd()."/uploads/$alias/";

to

$filepath = getcwd()."/uploads/testuser/";

 

... It works, and uploads the file to the directory "uploads/testuser". But I need to replace "testuser" with the value of $alias.

Link to comment
https://forums.phpfreaks.com/topic/170482-adding-variable-to-file-upload-path/
Share on other sites

I cant see the error because I'm using a Flash/AJAX script so the user is not actually being redirected to the page the upload script is on. The file is uploaded and shows the user a progress bar/confirmation message without having to refresh the page.

 

When I echo "$alias" and go to the page it shows the correct variable. So for some reason this:

$filepath = getcwd()."/uploads/$alias/";

... is not the correct syntax.

 

The file is being uploaded to /uploads/ instead of /uploads/$alias/ for some reason

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.