我有一个计算机科学的学校项目,现在我正在努力将多个图像上传到服务器并将路径添加到数据库。我一直在这里和其他地方寻找解决方案,但它们对我来说要么不完整,要么太复杂,这意味着我无法将它们"翻译"到我自己的情况中。我找到了一个脚本,用于将单个图片添加到服务器并将路径/名称添加到数据库,我已经对其进行了一些编辑:
<?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
}
}
}
}
?>
现在我希望有一种方法可以以某种方式组合这些脚本或找到另一种方法,在这种情况下,我可能需要显示我到底需要什么。(也许我应该...
谢谢
瓦西里斯
如果您的文件正在上传,那么您可以像此示例一样更新代码以将图片标题添加到数据库中....
<?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';
}
?>