我有一个脚本,可以将文件上传到服务器,并将文件名添加到数据库中。但是在上传之前,我想限制图片的最大尺寸。例如,如果我上传一张1000 x 500像素的图片,它将被限制为200 x 100像素,并保留其原始比例。但是,如果我上传一张300 x 300像素的图片,它将被限制为200 x 200像素。
<?php
//This is the directory where images will be saved
$target = "uploads/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
mysql_connect("hostname", "username", "password") or die(mysql_error()) ;
mysql_select_db("database") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `table` (name, photo) VALUES ('$name','$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";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
感谢您的帮助。
mysql_real_escape_string()
函数。 - ChrisW