0

Hi,

What I want to do is to upload a file to a specific directory as well as enter the data into a MySQL database.

The specified directory would be a value that has come from a previous select statement from the database.

So as an example, I would want a directory called Data/folder 1 and data/folder 2 ect ect. Its a bit difficult to explain really.

So would I need to already have the directories created or can I just create them from the value of the select statement on the fly then upload to the specified directory ?

Any help is much appreciated

Thanks

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Simply T
0

Hello SunnySideUp,

If I understood correctly, you need to upload files to a folder name you already have in another table..
You can easly query it before the upload and insert process starts.

But to avoid the repeated access to the database, I recommend creating a folder on the fly using the year+month or year+month+day each time you upload a file, like what WordPress is doing when uploading attachments, and then storing the file and folder names in seperate fields in the record..
Ofcourse you should check if the folder exist before trying to create it, for example:

$foldername = date('Ymd');

if(!(file_exists($foldername)))
{
	mkdir($foldername,0777);// What ever prevliges you need in the folder
}

move_uploaded_file($YourFileName, $foldername);

Hope this helped,
Chow,
T

Edited by Simply T: n/a

0

Hi Chow,

Thanks for your reply. Thats kind of the thing I was looking for, but how would I then upload files to a specific directory. For instance I have

if (move_uploaded_file($_FILES, {$_FILES})) {

How would I insert my new made directory name ?

Thanks

0

The move_uploaded_file() function require two parameters, the source file name, and the destination, when a looked back at it, I think my example wasn't clear :), it should have been:

move_uploaded_file($SourceFile, $DestinationAndName);

Where you can get the $SourceFile from the usual $_FILES, and the $DestinationAndName from joining the newly made destination folder name and file name, like $folderName.$_FILES.
But I strongly recommend using a random file name generation instead of the $_FILES to avoid overwriting error or unsafe file names..

For example:

// Use these couple of lines to get the uploaded file extension 
$fileType = strrchr($_FILES['image']['name'],".");
$fileType = strtolower($fileType);

//Use uniqid() or date('Ymdhis') to generate  a unique file name
$fileName = uniqid().$fileType;

//Then use them to upload your file to let's say your new folder $xFolder
move_uploaded_file($_FILES['image']['tmp_name'], $xFolder.$fileName)

Hope this helped,
T

Edited by Simply T: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.