Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将GD文件大小调整和重命名添加到php图像上载脚本_Php_Mysql_Image - Fatal编程技术网

将GD文件大小调整和重命名添加到php图像上载脚本

将GD文件大小调整和重命名添加到php图像上载脚本,php,mysql,image,Php,Mysql,Image,我有这个脚本,它允许上传图片,然后在数据库中存储指向文件系统上文件的指针。我从另一个StackOverflow问题中得到了这个脚本,它几乎完成了我需要的所有事情,除了两件事。 我需要做的第一件事是能够调整图像的大小,我刚刚读到的第二件事是重命名文件以防止用户错误等 这是一个上传文件,它从用户输入表单获取表单数据,并将数据写入DB,将图片写入images目录 <?php //This is the directory where images will be saved $target

我有这个脚本,它允许上传图片,然后在数据库中存储指向文件系统上文件的指针。我从另一个StackOverflow问题中得到了这个脚本,它几乎完成了我需要的所有事情,除了两件事。 我需要做的第一件事是能够调整图像的大小,我刚刚读到的第二件事是重命名文件以防止用户错误等

这是一个上传文件,它从用户输入表单获取表单数据,并将数据写入DB,将图片写入images目录

 <?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.";
}
?>

关于调整大小,这里有一篇文章解释如何使用GD将图像调整为任意大小。它具有裁剪或字母装箱选项,以适应目标纵横比

对于文件名,只需将文件重命名为随机id,并确保您尚未使用该id

do {
    $filename = uniqid().'jpg';
} while (file_exists('image/path/'.$filename);

你的问题到底是什么?请,请,了解准备好的语句和绑定变量。就目前而言,您的脚本很容易受到“SQL注入”攻击。请注意,如果不使用mysql\u real\u escape\u字符串,您的代码将非常不安全。您只需要更改行,如$name=$\u POST['namember'];to$name=mysql\u real\u escape\u字符串($\u POST['namember']);我仍然在学习php和mysql,这是我为自己设定的任务。我通过举例学习效果最好。自从我发布这个脚本以来,我发现它存在可用性问题,例如,它没有将文件类型限制为.gif、.jpg和.png文件。但是,它确实向我展示了如何将文件引用粘贴到数据库中