我有一个计算机科学的学校项目,现在我正在努力将多个图像上传到服务器并添加数据库的路径。我一直在这里和其他地方寻找解决方案,但它们对我来说要么不完整要么太复杂,这意味着我无法翻译'他们进入我自己的境地。我找到了一个脚本,用于向服务器添加单个图片并将路径/名称添加到数据库中,我已编辑了一下:
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);
$name = str_replace(' ', '_', $name);
$target = "images/";
$target = $target . $name . '/';
if (!file_exists($target))
{
mkdir('images/' . $name . '/', 0777, true);
}
$target = $target . basename( $_FILES['photo']['name']);
$file_type = $_FILES['photo']['type']; //returns the mimetype
//print ($file_type);
$allowed = array("image/jpeg", "image/gif", "image/png");
if(!in_array($file_type, $allowed)) {
$error_message = 'Only jpg, gif, and png files are allowed.';
$error = 'yes';
print($error_message);
}
else {
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("addimg") or die(mysql_error()) ;
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')");
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}
?>
另外,我发现了一个上传多个图片的脚本,但它没有把任何内容放到数据库中:
<?php
$valid_formats = array("jpg", "png", "gif", "jpeg");
//$max_file_size = 1024*100; //100 kb
$path = "images/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
?>
现在我希望有办法以某种方式组合这些脚本或找到另一种方式,在这种情况下,我可能需要显示我需要的是什么。 (也许我应该&#39;无论如何......)
谢谢,
瓦西利斯
答案 0 :(得分:0)
如果您的文件正在上传,那么您可以像这个示例一样更新您的代码,将图片标题添加到数据库....
<?php
$valid_formats = array("jpg", "png", "gif", "jpeg");
//$max_file_size = 1024*100; //100 kb
$path = "images/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$name = str_replace(' ', '_', $name);
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
// Insert into Database
$pic = $path.$name;
$query = mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") or die(mysql_error());
if($query) {
$count++; // Number of successfully uploaded file
}
}
}
}
echo $count .' Files uploaded';
}
?>