dtiopy6088 2013-09-03 16:32
浏览 43

上传时更改名称

I’m updating my admin side of my website.

I have currently managed to include an add page which uploads to the database for my site to display the content.

Within this page is an image upload button.

When I select a file from this upload button it saves the image to my /images folder.

However. When I upload images using this feature on my ipad, It uploads ALL images as image.jpg.

What I need now is this:

I need my uploaded images to save to the name of the brand and not the original uploaded image name. So if I upload an "image.jpg", then enter APPLE as the brand, I want the image to save as apple.jpg as I upload the file.

my current add.php page is this:

<?php

session_start();

include_once('../include/connection.php');

if (isset($_SESSION['logged_in'])){
  if (isset($_POST['title'], $_POST['content'])) {
        $title   = $_POST['title'];
        $content = nl2br($_POST['content']);
        if (!empty($_POST['image']))
        {
            $image = $_POST['image'];
        }
        else
        {
            $image = $_POST['imageupload'];

            if (isset($_FILES['imageupload']))
            {
              $errors = array();
              $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');

              $file_name = $_FILES['imageupload'] ['name'];
              $file_ext = strtolower (end (explode ('.', $file_name)));
              $file_size = $_FILES['imageupload'] ['size'];
              $file_tmp = $_FILES['imageupload'] ['tmp_name'];

              if (in_array ($file_ext, $allowed_ext) === false) {
                     $errors[] = 'File extension not allowed';
              }

              if ($file_size > 2097152) {
                     $errors[] = 'File size must be under 2mb';
              }

              if (empty($errors)) {
                     if (move_uploaded_file($file_tmp, 'images/'.$file_name)) {
                           echo 'File uploaded';
                           $image = 'http://www.xclo.mobi/xclo2/admin/images/'.$file_name;
              }
              }else{
                    foreach ($errors as $error)
                    echo $error, '<br />';
              }

            }
        }
        $link     = $_POST['link'];
        $category = $_POST['category'];
        $brand    = $_POST['brand'];


if (empty($title) or empty($content)) {
         $error = 'All Fields Are Required!';
}else{
 $query = $pdo->prepare('INSERT INTO mobi (promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) VALUES(?, ?, ?, ?, ?, ?)');
 $query->bindValue(1, $title);
 $query->bindValue(2, $content);
 $query->bindValue(3, $image);
 $query->bindValue(4, $link);
 $query->bindValue(5, $category);
 $query->bindValue(6, $brand);



     $query->execute();
    header('location: index.php');
}

}
          ?>
    <?php

if (isset($_FILES['Filedata']))
{
// And if it was ok
    if ($_FILES['Filedata']['error'] !== UPLOAD_ERR_OK)
    exit('Upload failed. Error code: ' . $_FILES['image']['error']);

    $filename = $_FILES['Filedata']['name'];
    $targetpath    = "../img/news/" . $filename; //target directory relative to script location

    $copy = copy($_FILES['Filedata']['tmp_name'], $targetpath);
}
?>

<html>
<head>
<title>Add Article</title>
<link rel="stylesheet" href="../other.css" />
</head>

<body>
<div class="container">
<a href="index.php" id="logo"><b>&larr; Back</b></a>

<br />

<div align="center">
<h4>Add Article</h4>

<?php if (isset($error)) { ?>
     <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>

<form action="" method="post" autocomplete="off" enctype="multipart/form-data">

<input type="text" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br />
<input name="imageupload" type="file" id="image" placeholder="Imageupload" />
<input type="text" name="image" placeholder="Image" /><br /><br />
<input type="link" name="link" placeholder="Link" /><br /><br />
<input type="category" name="category" placeholder="Category" /><br /><br />
<input type="category" name="brand" placeholder="Brand" /><br /><br />
<input type="submit" value="Add Article" />

</form>
</div>
</div>
</body>
</html>


<?php
}else{
       header('location: index.php');
}

?>

please can someone help? thank you.

  • 写回答

3条回答 默认 最新

  • douduan3203 2013-09-03 16:53
    关注

    During the upload process then you need to change the filename to the new filename you want it to be.

    {
        $errors = array();
        $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
    
        $file_name = $_FILES['imageupload'] ['name'];
        $file_ext = strtolower (end (explode ('.', $file_name)));
        $file_size = $_FILES['imageupload'] ['size'];
        $file_tmp = $_FILES['imageupload'] ['tmp_name'];
    
        $file_name = $_POST['brand'].'.'.$file_ext;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序