需要一个表单进程自动重新命名上传照片
我有一个PHP缓存代码的名字“我的形式生成的html的网页的基础上,在表单字段的一个数据我形成。
<!--Start Buffer->
<?php ob_start(); $URL=preg_replace('#[^_0-9a-zA-Z]#', '_', $_REQUEST['timeValue']); ?>
即特定形式字段是自动填充与当前UNIX毫秒时钟。
<!--FormPage-->
<!--Timevalue/Unix millisecond code/field->
<div class="formElement">TIMESTAMP URL<br><input id="timeValue" name="timeValue"></div>
<script src="timeValueURL.js"></script>
所以......我的.html文件最终保存为“名”(即数字),这是因为无论“毫秒”是在被“提交”我的形式时刻相同。
*注意:我的“提交”按钮也会在提交时更新毫秒时钟。
生成的.html文件然后保存到其中一个有效的目录中; '类别/新闻','类别/运动'或'类别/娱乐'。 (例如)如果.html文件被保存到'news'目录中......并且当表单被提交时毫秒时钟为'1500000000000'... .html文件将有一个url “mywebsite.com/categories/news/1500000000000.html”
在相同的形式......我有我的观众从自己的服务器/设备上传的图像文件的文件上传字段。
注意:允许的图像文件类型仅限于JPEG,PNG和GIF。
这些上传的图像保存在名为'categories-images'的文件夹中。
目前......这些图像文件保存到他们上传时的“名称”下的那个文件夹中。
示例:如果用户上传名为'blueimage.jpg'的JPEG图像,则该文件将保存在URL'mywebsite.com/categories/category-images/blueimage.jpg'中。
相反...我想让图像文件与.html文件共享相同的“名称”。
因此,使用上面的例子...图像应该有“1500000000000.jpg”的名称,并有
“mywebsite.com/categories/category-images/1500000000000.jpg”的URL地址。
问题:
有没有办法实现这个类似,甚至可能是在引入命名.html文件中使用的代码?
注:
它发生,我认为我应该包括图像处理以及PHP的。
所以这里是....
<?php
$target_dir = "articles/article-images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"]))
{
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false)
{
echo "" . $check[""] . ""; $uploadOk = 1;
}
else
{
echo " × FILE IS NOT AN IMAGE"; $uploadOk = 0;
}
}
if(file_exists($target_file))
{
echo " × THIS IMAGE ALREADY EXIST ON SERVER"; $uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000)
{
echo " × FILE IS TOO LARGE"; $uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif")
{
echo " × ONLY JPG, JPEG, PNG & GIF FILES ARE PERMITTED"; $uploadOk = 0;
}
if ($uploadOk == 0)
{
echo " × IMAGE WAS NOT UPLOADED";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo '<img class="fixed-ratio-resize" src="../../articles/article-images/'. basename($_FILES["fileToUpload"]["name"]). '">';
}
else
{
echo " × IMAGE WAS NOT UPLOADED";
}
}
?>
你应该只需要采取的HTML文件(毫秒,在以往任何时候读取的代码为)的名称和更换$_FILES["fileToUpload"]["name"]
。我将把该值作为$millisecs
作为参考,其扩展名为$imageFileType
:
<?php
# Presumably this whole thing is based off a post, so just check at the top
if(!isset($_POST["submit"])) {
# Let the user know, this this can not be accessed without a submission
die('Nothing submitted.');
}
# Set the extension first since you aren't going to use the name
$imageFileType = pathinfo($_FILES["fileToUpload"]["name"],PATHINFO_EXTENSION);
# Set dir
$target_dir = "articles/article-images/";
# Add your milliseconds carried over from your html script. Add extension,
# this will be the new name
$target_file = $target_dir.$millisecs.".{$imageFileType}";
# Default affermative
$uploadOk = 1;
# Get image size
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
# I am not suer what this is...it doesn't look like you should echo
# anything at all...
echo "".$check[""]."";
$uploadOk = 1;
}
else {
echo " × FILE IS NOT AN IMAGE";
$uploadOk = 0;
}
if(file_exists($target_file)) {
echo " × THIS IMAGE ALREADY EXIST ON SERVER";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo " × FILE IS TOO LARGE";
$uploadOk = 0;
}
# Set allowed file types
$allowed = array('jpg','jpeg','png','gif');
#Check if type is in list
if(!in_array(strtolower($imageFileType),$allowed)) {
echo " × ONLY JPG/JPEG, PNG & GIF FILES ARE PERMITTED";
$uploadOk = 0;
}
if($uploadOk == 0) {
echo " × IMAGE WAS NOT UPLOADED";
}
else {
# Move the temp file to the new name here
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
# Write the basename of the new file here
echo '<img class="fixed-ratio-resize" src="../../articles/article-images/'. basename($target_file). '">';
}
else {
echo " × IMAGE WAS NOT UPLOADED";
}
}