简体   繁体   English

在虚拟主机上运行的 PHP 不会为图像创建目录

[英]PHP running on virtual host doesn't create directory for images

This is my httpd-vhost.conf这是我的 httpd-vhost.conf

<VirtualHost *:80>
    DocumentRoot "/opt/lampp/htdocs/php-crash-course-2020/14_product_crud/03_good/public"
    ServerName products.test
    <Directory "/opt/lampp/htdocs/php-crash-course-2020/14_product_crud/03_good/public">
    Options All
    AllowOverride All
    Require all granted
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f  
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    </Directory> 
</VirtualHost>

I have given all privileges to this folder with我已授予此文件夹的所有权限

sudo chown -R $USER /opt/lampp/htdocs/

Everything works fine if there is already an images folder inside the public folder but when I want to create a new product (image, title, description, price) it creates a product in the database with the correct image path but it does not create an images folder.如果公用文件夹中已经有一个图像文件夹,那么一切正常,但是当我想创建一个新产品(图像、标题、描述、价格)时,它会在数据库中创建一个具有正确图像路径的产品,但它不会创建一个图片文件夹。

This is code for creating an images folder inside the public folder这是在公用文件夹中创建图像文件夹的代码

if (!is_dir(__DIR__ . '/../public/images')) {
    mkdir(__DIR__ . '/../public/images');
}

This is code for moving an image file inside the images folder这是用于在图像文件夹中移动图像文件的代码

if ($this->imageFile && $this->imageFile['tmp_name']) {

    if ($this->imagePath) {

        unlink(__DIR__ . '/../public/' . $this->imagePath);
    }
    $this->imagePath = 'images/' . UtilHelper::randomString(8) . '/' . $this->imageFile['name'];

    mkdir(dirname(__DIR__ . '/../public/' . $this->imagePath));
    move_uploaded_file($this->imageFile['tmp_name'], __DIR__ . '/../public/' . $this->imagePath);
}

If I already have the images folder and there is the image for a certain product, the image can be loaded on the website and I can delete that product but when I delete it, the image inside the images folder is not deleted but it should be.如果我已经有 images 文件夹并且有某个产品的图像,则可以在网站上加载图像,我可以删除该产品,但是当我删除它时,图像文件夹中的图像不会被删除,但应该是.

The application works when I run it on a PHP server as当我在 PHP 服务器上运行该应用程序时

php -S localhost:8080

Inside httpd.conf User is daemon and Group is daemon.在 httpd.conf 里面,用户是守护进程,组是守护进程。 Without editing httpd.conf file I ran this command.在没有编辑 httpd.conf 文件的情况下,我运行了这个命令。

chown -R $USER:daemon /opt/lampp/htdocs/ chown -R $USER:daemon /opt/lampp/htdocs/

It solved the problem.它解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM