上传时更改名称

时间:2013-09-03 16:32:10

标签: php image file-upload content-management-system

我正在更新我网站的管理员端。

我目前设法包含一个添加页面,该页面上传到我的网站的数据库以显示内容。

在此页面中是图像上传按钮。

当我从此上传按钮中选择文件时,它会将图像保存到我的/ images文件夹中。

然而。当我在ipad上使用此功能上传图像时,它会将所有图像上传为image.jpg。

我现在需要的是:

我需要将上传的图片保存到品牌名称而不是原始上传的图片名称。因此,如果我上传“image.jpg”,然后输入APPLE作为品牌,我希望在我上传文件时将图像保存为apple.jpg。

我当前的add.php页面是这样的:

<?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');
}

?>

请有人帮忙吗?谢谢。

3 个答案:

答案 0 :(得分:0)

在上传过程中,您需要将文件名更改为您希望的文件名。

{
    $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;

答案 1 :(得分:0)

我使用了这段代码,它对我来说很好用。

谢谢你。

$brand = $_POST['brand']; // Put this up by where you define $title


$up_file = $brand.'.'.$file_ext;
if (move_uploaded_file($file_tmp, 'images/'.$up_file)) {
echo 'File uploaded';
$image = 'www.mysite.com/images/'.$up_file;
}

请1up ......

答案 2 :(得分:0)

1)获取图片扩展名

$ ext = pathinfo($ filename,PATHINFO_EXTENSION);

2)通过concatenatin品牌名称上传您的文件&amp;扩展

move_uploaded_file($ _ FILES ['imageupload'] ['tmp_name'],$ _POST ['your_brand']。'。'。$ ext);