我有这个脚本允许上传图片,然后指针存储在DB中文件系统上的文件中。我从另一个StackOverflow问题得到了这个脚本,除了两件事之外,它几乎可以做我需要的一切。 我需要的第一件事是能够调整图像的大小,我刚才读到的第二部分是重命名文件以防止用户错误等。
这是上传文件,它从用户输入表单中获取表单数据,并将数据写入DB,将图片写入图像目录。
<?php
//This is the directory where images will be saved
$target = "your directory";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];
$bands=$_POST['otherBands'];
// Connects to your Database
mysql_connect("yourhost", "username", "password") or die(mysql_error()) ;
mysql_select_db("dbName") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;
//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.";
}
?>
答案 0 :(得分:0)
为了调整大小,这里有一篇文章解释使用GD将图像大小调整为任意大小。它具有裁剪或字母装箱选项以适合目标纵横比。
http://www.spotlesswebdesign.com/blog.php?id=1
对于文件名,只需将文件重命名为随机ID,并确保您尚未使用该ID。
do {
$filename = uniqid().'jpg';
} while (file_exists('image/path/'.$filename);