Uploading to dynamic folder, but download files don't list.
I want it to upload to different folders depending on the user that's logged in.
I used the demo and modified it to my liking, uploading works, deleting works, but if I refresh the page it doesn't list the already uploaded files.
It works fine because I added a hidden div within the "fileupload" form which submits whenever a new file is uploaded, but where can i pass in my variable when the page loads and lists current downloads?
Here is how I accomplished what I have so far.
I edited the index.html to my liking and added a hidden div within the "fileupload" form like this
<form id="fileupload" action="server/php/" method="POST" enctype="multipart/form-data">
<input type="hidden" name="foid" id="foid" value="<?php echo $userarray[ID]; ?>"/>
I've edited upload.class.php and added the following to have it upload to the appropriate users's directory.
function __construct($options=null) { if(!is_dir('files/'.$_REQUEST['foid'])){ mkdir('files/'.$_REQUEST['foid']); } $this->options = array( 'script_url' => $this->getFullUrl().'/', //'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/19/', //'upload_url' => $this->getFullUrl().'/files/19/', 'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/'.$_REQUEST['foid'].'/', 'upload_url' => $this->getFullUrl().'/files/'.$_REQUEST['foid'].'/',
... etc.
I modified set_file_delete_url to this:
protected function set_file_delete_url($file) { $file->delete_url = $this->options['script_url'] .'?foid='.$_REQUEST['foid'].'&file='.rawurlencode($file->name); $file->delete_type = $this->options['delete_type']; if ($file->delete_type !== 'DELETE') { $file->delete_url .= '&_method=DELETE'; } }
js/main.js
// Load existing files:
$('#fileupload').each(function () {
var that = this;
$.getJSON(this.action, function (result) {
if (result && result.length) {
$(that).fileupload('option', 'done')
.call(that, null, {result: result});
}
});
});
You would need to pass the foid there too as thats where the request for the file list is made.
I think you are going about this the wrong way though. This allows clients to specify the folder by editing their postdata. You should use sessions in upload.class.php and use $_SESSION['foid'] not $_REQUEST['foid']. That way it can not be changed by the user and its actually less work for you.
Please read: https://github.com/blueimp/jQuery-File-Upload#support