【问题标题】:Image uploads to folder with php使用php将图像上传到文件夹
【发布时间】:2021-09-29 02:06:05
【问题描述】:

我有一个带有 php 的图像上传脚本,它可以在 localhost 服务器中工作,但是当我将网站上传到我的域服务器时,它会上传图像,但不会在 spathfic 文件夹中,我将其设置得很好,将文件夹路径设置为图片名称如

路径是

admin\upload\novelimage

图片名称会是这样的

upload\novelimage\...image that uploaded

如果脚本或服务器出错,我不知道,但我没有看到他们那边需要上传权限,所以我猜问题出在脚本上 这是脚本

  //upload files
            $avatarname = $_FILES['NovelImage']['name'];
            $avatarsize = $_FILES['NovelImage']['size'];
            $avatartmp = $_FILES['NovelImage']['tmp_name'];
            $avatartype = $_FILES['NovelImage']['type'];

            $avatarAllowedextention = array("jpeg", "jpg", "png", "gif");

            //get avatar extention

            $avatarextention = strtolower(end(explode('.', $avatarname)));
              #get the varibles from the form 
              $Novelname = $_POST['Novelname'];
              $Describe = $_POST['Describe'];
              $category = $_POST['langOpt3'];
              // condtion ? true and false
                  //check username
                  $formerrors = array();
                  if (! empty($avatarname) && ! in_array($avatarextention, $avatarAllowedextention)) {
                    $formerrors[] = 'avatar extension is not allowed';
                }
                if (empty($avatarname)) {
                    $formerrors[] = 'empty avatar is not allowed';
                }
                if ($avatarsize > 4194304) {
                    $formerrors[] = 'avatar size is bigger than 4mb';
                }
                foreach ($formerrors as $error) {
                    echo '<div class="alert alert-danger">' . $error . '</div>';
                }
                if (empty($formerrors)) {
                    $avatar = rand(0, 1000000) . '_' . $avatarname;

                    move_uploaded_file($avatartmp, "upload\novelimage\\" . $avatar);

                  $check = checkitem("NovelName", "novel", $Novelname);

                  if ($check == 1) {
                      $theMsg = "<div class='alert alert-danger'>sorry this user name is exit</div>";

                      redirecthome($theMsg, 'back');
                  }else{
                        //insert userinfo into database whith this info
                        $stmt = $con->prepare("INSERT INTO novel(NovelName, NovelImage, description) 
                        VALUES(:znovel, :zNovelImage, :zdescription)");
                        $stmt->execute(array(
                            'znovel' => $Novelname,
                            'zNovelImage' => $avatar,
                            'zdescription' => $Describe
                        ));
                        $lastid = $con->LastInsertId();
                        $stmt = $con->prepare("INSERT INTO book_category(novelid, categoryID) 
                        VALUES(:znovelid, :zcategoryID)");
                        foreach ($category as $cat) {
                            $stmt->execute(array(
                                'znovelid' => $lastid,
                               'zcategoryID' => $cat
                             ));   
                        }
                    }

                        
                    //erg
                    $theMsg = "<div class='alert alert-success '>" . $stmt->rowCount(). 'Record Updated</div>'; 

                    redirecthome($theMsg, 'back', 100);
                  } 

我真的看不出有什么问题,请帮忙

【问题讨论】:

  • 您需要比“一些错误”更具体。见How to Ask

标签: php image upload


【解决方案1】:

你的问题不是很清楚, 但要确认这一点“您的图像未上传到服务器中您想要的特定路径” 根据您的路径代码,我假设您的本地主机在 Windows 操作系统中运行, 从问题来看,我认为您的服务器是 Linux 服务器。 因此,Windows BackSlash('') 无法被 Linux 的 Front Slash 识别。

if (empty($formerrors)) {
    $avatar = rand(0, 1000000) . '_' . $avatarname;
    move_uploaded_file($avatartmp, "upload\novelimage\\" . $avatar);
    $check = checkitem("NovelName", "novel", $Novelname);

这将是:

move_uploaded_file($avatartmp, "upload/novelimage/\" . $avatar);

您可以参考与 Linux 服务器中的 Windows 路径问题相关的 StackOverflow 链接: Help with windows path - PHP

我希望这对你有帮助,但你的问题对我来说不是很清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    相关资源
    最近更新 更多