0

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

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

此页面内是一个图像上传按钮。

当我从此上传按钮中选择一个文件时,它会将图像保存到我的 /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');
}

?>

请问有人可以帮忙吗?谢谢你。

4

3 回答 3

0

1) 获取图片扩展名

$ext = pathinfo($filename, PATHINFO_EXTENSION);

2)通过连接您的品牌名称和扩展名上传您的文件

move_uploaded_file($_FILES['imageupload'] ['tmp_name'], $_POST['your_brand'].'.'.$ext);

于 2013-09-03T17:02:18.787 回答
0

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;
于 2013-09-03T16:53:21.137 回答
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;
}

请1上...

于 2013-09-03T16:59:53.127 回答