我正在上传图像和裁剪到一个目录,我如何添加一个文本字段来更改上传时的文件名?
我曾尝试将名称字段添加到表单中,但无法在上传时获取表单以发布新名称。
<form action="" method="POST" enctype="multipart/form-data">
<input id="input-6" name="art[]" type="file" multiple class="file-
loading">
<input type="text" name="newname"/>
<input type="submit" name="sub">
</form>
<?php
if(isset($_POST['sub'])){ (my guess is that "sub" needs to change to
"newname" but that does not seem to work)
if(isset($_FILES['art'])){
foreach ($_FILES["art"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["art"]["tmp_name"][$key];
$name = $_FILES["art"]["name"][$key];
$image = imagecreatefromstring(file_get_contents(($tmp_name)));
$filename = 'newimage/'.$name.'';
}
?>没有错误消息只是没有附加文件名。
发布于 2019-07-24 00:17:09
您遗漏了实际上传的整个部分(大多数基本示例都涉及到使用move_uploaded_file)。目前,您的脚本只是请求数据(二进制文件和新名称)并对其进行解析,而不执行任何操作。一旦你得到了数据,你就需要上传数据。在该步骤中,您可以编辑文件名。
例如(取自https://www.tutorialspoint.com/php/php_file_uploading,修改为包含新的name字段):
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_POST['newname'] || $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="text" name="newname" />
<input type="submit"/>
</form>
</body>
</html>
?>发布于 2019-07-24 00:21:05
您的名字将存储在$_POST['newname']中,因此您可以更改行
$name = $_FILES["art"]["name"][$key];至
$name = $_POST['newname'];但是有一个问题,您允许发送多个文件(输入中的属性为multiple ),但您只有一个文本字段来写入一个文件名。
你猜到的这一行:
if(isset($_POST['sub']))如果您通过单击名称为"sub“的按钮发送数据(按enter也被视为单击默认按钮),则会进行测试。
发布于 2019-07-24 00:48:26
我想我可能会放置完整的工作示例,感谢所有人的帮助,也许有一种方法可以改进这一点,但它是有效的
<form action="" method="POST" enctype="multipart/form-data">
<input id="input-6" name="art[]" type="file" multiple class="file-loading">
<input type="text" name="newname"/>
<input type="submit" name="sub">
</form>
<?php
if(isset($_POST['sub'])){
if(isset($_FILES['art'])){
foreach ($_FILES["art"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["art"]["tmp_name"][$key];
//$name = $_FILES["art"]["name"][$key];
$name = $_POST['newname'];
$image = imagecreatefromstring(file_get_contents(($tmp_name)));
$filename = 'newimage/'.$name.'.jpg';
$thumb_width = 300;
$thumb_height = 300;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image
horizontally
0 - ($new_height - $thumb_height) / 2, // Center the
image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}
}
}
header("Location: assignmentart.php");} ?>
https://stackoverflow.com/questions/57168224
复制相似问题