我正在尝试允许用户将2个图像上传到注册时创建的文件夹,并在上传后将这些文件的后续路径插入到我的表中,其中“main”作为第一个图像路径,并且'one'作为下一个图像路径。
上传程序正确地将两个文件上传到文件夹中,但我是否执行INSERT将图像插入“main”中的一个路径,并将两个路径映像为“one”?
以下代码用于上传....
非常感谢任何帮助!
<?php
$userid = $_SESSION['userid'];
$username = $_SESSION['user_name'];
$uploadDir = "images/test/$username/";
$uploadsNeeded = '2';
for($i = 0; $i < $uploadsNeeded; $i++){
$file_name = $_FILES['uploadFile'. $i]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = copy($_FILES['uploadFile'. $i]['tmp_name'],$uploadDir . $file_name);
// prompt if successfully copied
if($copy){
echo "$file_name | uploaded sucessfully!<br>";
}else{
echo "$file_name | could not be uploaded!<br>";
}
$filePath = $uploadDir . $file_name;
$sql = "UPDATE images SET main = '$filePath' WHERE userid = '$_SESSION[userid]'";
$result = @mysql_query($sql, $connection) or die(mysql_error());
$sql = "UPDATE images SET one = '$filePath' WHERE userid = '$_SESSION[userid]'";
$result = @mysql_query($sql, $connection) or die(mysql_error());
}
?>
答案 0 :(得分:0)
我想这就是你需要做的......
$ userid = $ _SESSION [&#39; userid&#39;];
$ username = $ _SESSION [&#39; user_name&#39;];
$ uploadDir =&#34; images / test / $ username /&#34 ;;
$ filePath = array();
$ uploadsNeeded =&#39; 2&#39;;
for($ i = 0; $ i&lt; $ uploadsNeeded; $ i ++){
$ file_name = $ _FILES [&#39; uploadFile&#39;。 $ I] [&#39;名称&#39;];
//剥离斜杠的文件名
$ file_name = stripslashes($ file_name);
$ file_name = str_replace(&#34;&#39;&#34;,&#34;&#34;,$ file_name);
$ copy = copy($ _ FILES [&#39; uploadFile&#39;。$ i] [&#39; tmp_name&#39;],$ uploadDir。$ file_name);
//如果成功复制则提示
如果($拷贝){
echo&#34; $ file_name |上传成功了!
&#34 ;;
} 否则{
echo&#34; $ file_name |无法上传!
&#34;;
}
$ filePath [] = $ uploadDir。 $ FILE_NAME;
}
$ sql =&#34; UPDATE图像SET main =&#39; $ filePath [0]&#39; AND one =&#39; $ filePath [1]&#39; WHERE userid =&#39; $ _ SESSION [userid]&#39;&#34;;
$result = @mysql_query($sql, $connection) or die(mysql_error());