Reputation: 2277
Im stuck on a problem,
I have created a multiply uploader, it works fine until i want the file I upload to register the name inside a database, The error message I get is
Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\wamp\www\bookstyled\profile.php on line 16
My line 16 is the variable file_name
$file_name = mysql_real_escape_string($_FILES['file_name']['name']);
If I remove the mysql_real_escape_string, It actually save to the database but not as the file name, but its says " Array "
This is some of the code
if(isset($_FILES['file_name'])) {
foreach ($_FILES['file_name'] ['tmp_name'] as $key => $tmp_name){
$file_name = mysql_real_escape_string($_FILES['file_name']['name']);
$dt1=date('y-m-d H:m:s');
$ip=$_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `files` (`file_name`, `user_name`,`file_time`,`file_ip`) VALUES ('$file_name', '{$_SESSION['username']}','$dt1','$ip')") ;
move_uploaded_file($tmp_name, "core/files/{$_FILES['file_name']['name'][$key]}");
}
}
And If I didn't mention it The files are being upload.
Thanks
Upvotes: 1
Views: 450
Reputation: 120
You have enabled multiple uploads. So I'm guessing your html names for the fields are arrays. eg:
<input type="file" name="file_name[]" multiple="multiple">
Now $_FILES['file_name']['name'] doesn't hold one file but multiple files in an array.
Each file is individually accessed through
$_FILES['file_name']['name'][$i] //where $i is a 0,1,2.....
Since you are using
$file_name = mysql_real_escape_string($_FILES['file_name']['name'])
the function mysql_real_escape_string isn't being given a string as the parameter but the complete array which hold each and every file uploaded to 'file_name'.
The solutions is simple, you need to use
file_name = mysql_real_escape_string($_FILES['file_name']['name'][$key])
$key because I see that you are already using that in move_upload_file function
Upvotes: 3
Reputation: 11
Looks like $_FILES['file_name']['name'] is an array as opposed to a string, so try $_FILES['file_name']['name']['key']
Upvotes: 1
Reputation: 4690
Untested, but could work.
if(isset($_FILES['file_name'])) {
foreach ($_FILES['file_name'] as $file){
$file_name = mysql_real_escape_string($file['tmp_name']);
$dt1=date('y-m-d H:m:s');
$ip=$_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `files` (`file_name`, `user_name`,`file_time`,`file_ip`) VALUES ('$file_name', '{$_SESSION['username']}','$dt1','$ip')") ;
move_uploaded_file($tmp_name, "core/files/{$file['name']}");
}
}
Upvotes: 0
Reputation: 21475
If you change this
$file_name = mysql_real_escape_string($_FILES['file_name']['name']);
to this
$file_name = mysql_real_escape_string($_FILES['file_name']['name'][$key]);
As you use this [$key]
in this sentence move_uploaded_file($tmp_name, "core/files/{$_FILES['file_name']['name'][$key]}");
I think you should use on the line 16 too.
Upvotes: 1