首页 > 解决方案 > 使用php将图像上传到文件夹

问题描述

我有一个带有 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);
                  } 

我真的在这里看不到任何问题,请帮忙

标签: phpimageupload

解决方案


你的问题不是很清楚,但要确认这个“你的图像没有上传到你想要在服务器中的特定路径”从你的路径代码中,我假设你的本地主机在 Windows 操作系统中运行,从这个问题我认为你的服务器是 Linux 服务器。因此,Linux 的正斜杠无法识别 Windows BackSlash('')。

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

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


推荐阅读