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

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.