views:

336

answers:

2

I want to insert files into mysql from php function. The files that need to be uploaded are already present on the server, so I don't want to use the upload form.

I want to loop through the directory and get the files info into $_FILES.

Please let me know how I will get the $file into $_FILES and then call upload.

$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {
  echo "$file";
}
A: 

If the files are already on the server then they don't need to be "uploaded". They just need to be moved.

You could try something like...

$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {
    // example query may be...
    $sql = sprintf("INSERT INTO table SET filename = %s", mysql_escape_string($path . "/" . $file));
    mysql_query($sql);
}
Chris Gutierrez
Hi Chris, I want to upload the files into mysql DB(blobs). The fuction already exists. Now I want to populate the $filePath with the file data and call the upload fuction..$fileName = filter_var($_POST["name"], FILTER_SANITIZE_STRING);$filePath = filter_var($_FILES['Filedata']['tmp_name'], FILTER_SANITIZE_STRING);echo File::upload($pid, $fileName, $filePath);
JadaBharata
If you are wanting to get the contents of the file then you could do what Pekka mentioned and just use file_get_contents or the fopen/fread combo then add it to your insert
Chris Gutierrez
A: 

$_FILES is solely for handling files uploaded to the script by the client. You should be able to insert the file contents using file_get_contents() or fopen()/fread() without further ado.

Pekka
Hi Pekka, I have to actually load that file as blob in mysql. There is a function which is already doing it. I have to just call it.$fileName = $_POST["name"]; $filePath = $_FILES['Filedata']['tmp_name']; echo File::upload($fileName, $filePath);I want to use the $filePath..
JadaBharata
$file in your loop should be identical to $filePath from your comment.
Pekka