file_put_contents failed in storing image into directory

Jyotishgher Astrology
By -
0

 

file_put_contents failed in storing image into director

What I did go to terminal and the directory pic where i have folder with the name imagesphoto:

[android@lamp pic]$ sudo chcon -t httpd_sys_rw_content_t imagesphoto/


sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/html/.*"


httpd_sys_content_t
Use this type for static web content, such as .html files used by a static website. Files labeled with this type are accessible (read only) to httpd and scripts executed by httpd. By default, files and directories labeled with this type cannot be written to or modified by httpd or other processes. Note that by default, files created in or copied into /var/www/html/ are labeled with the httpd_sys_content_t type.


The simple solution

We just changed the security context for the folders and files to httpd_sys_rw_content_t which allows apache web server to edit the files:

httpd_sys_rw_content_t
Files labeled with this type can be written to by scripts labeled with the httpd_sys_script_exec_t type, but cannot be modified by scripts labeled with any other type. You must use the httpd_sys_rw_content_t type to label files that will be read from and written to by scripts labeled with the httpd_sys_script_exec_t type.

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/managing_confined_services/sect-managing_confined_services-the_apache_http_server-types
1
chcon -R --type httpd_sys_rw_content_t /var/www/example.com;



The error message "failed to open stream: Permission denied for put_file_contents" typically occurs in a LAMP (Linux, Apache, MySQL, PHP) server environment when PHP doesn't have the necessary permissions to write to a file or directory.

Here are some steps you can follow to troubleshoot and resolve the issue:

  1. Check File Permissions: Ensure that the file or directory you are trying to write to has the appropriate permissions that allow PHP to write. You can use the ls command in the terminal to check the current permissions:

    bash
    ls -l /path/to/your/file_or_directory

    The output will display the permissions in a format like this:

    sql
    -rw-r--r-- 1 user group 1024 Aug 2 10:00 your_file.txt drwxr-xr-x 2 user group 4096 Aug 2 10:00 your_directory/

    Make sure that the file or directory has write permissions for the appropriate user or group. You can modify the permissions using the chmod command:

    bash
    chmod +w /path/to/your/file_or_directory
  2. Verify Ownership: Ensure that the file or directory is owned by the correct user and group. If PHP is running as the Apache user (often www-data on Ubuntu), then the file/directory should be owned by that user.

    bash
    chown www-data:www-data /path/to/your/file_or_directory
  3. SELinux or AppArmor (if applicable): If you're using SELinux or AppArmor on your server, they might be preventing PHP from writing to certain locations. Check the security context of the file/directory and modify it if necessary:

    bash
    # For SELinux (CentOS, RHEL, Fedora) chcon -t httpd_sys_rw_content_t /path/to/your/file_or_directory # For AppArmor (Ubuntu) aa-complain /etc/apparmor.d/php
  4. Check Disk Space: Ensure that the disk where the file is located has enough free space to accommodate the new data.

  5. Restart Services: After making changes to file permissions or ownership, restart the Apache web server and PHP-FPM (if applicable) to apply the changes.

    bash
    sudo systemctl restart apache2 sudo systemctl restart php-fpm
  6. Debugging: If the issue persists, you can enable PHP error reporting to get more detailed information about the error. Update your PHP configuration (php.ini) and set the following directives:

    ini
    display_errors = On error_reporting = E_ALL

    After making these changes, restart your web server.

Remember that modifying file permissions can have security implications, so make sure you are granting access only to the necessary files and directories. Avoid using overly permissive settings (e.g., chmod 777) unless it is absolutely required and you understand the risks. Always prioritize security best practices when configuring your server.


<?php

$conn = oci_connect('xxx', 'xxx', 'xxx');  
    
       $ITEMID=$_REQUEST['ITEMID'];
        
       $upload=$_REQUEST['upload'];
    
                   file_put_contents("imagesphoto/".$filename,base64_decode($upload));

                    $qry="INSERT into  sp_item_photo(ITEMID,PHOTO1,PHOTO2,TDY_DT) values ('$ITEMID','$filename',null,SYSDATE)";
            
                    $sqlu  = oci_parse($conn, $qry);
             oci_execute($sqlu,OCI_COMMIT_ON_SUCCESS) or die(oci_error($sqlu));
                        if($sqlu==true)
             echo "File Uploaded Successfully".$filename;           else
             echo "Could not upload File"; ?>

Post a Comment

0Comments

Post a Comment (0)