dongyan6503 2012-07-10 19:19
浏览 10
已采纳

多个上传php

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

  • 写回答

4条回答 默认 最新

  • duanchi4544 2012-07-10 19:40
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大