Jump to content

move to file to a folder in prestashop 1.7 controller


jafakash cp

Recommended Posts

hi, 

 

am working on prestashop 1.7 . i made a tpl  file with form to upload image . when button submit i will move upload image to an image folder and call email functionality to add attachment , my email functionality is working fine but in controller (i have added my own controller) i cant move image to a particular folder.

 

my code to move uploaded file is given below.

 

******************************************************************************************************

if (isset($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['tmp_name']))
{
$move =$url.__PS_BASE_URI__.'/img/';
//echo __PS_BASE_URI__;die;
 
$uploadedFile = $_FILES['fileUpload']['tmp_name'];
if(move_uploaded_file($uploadedFile, $url.__PS_BASE_URI__.'/img/'.$_FILES["fileUpload"]["name"]))
{
 
chmod($move, 0755);
   echo "upload complete";die;
}
else{echo "     1error";die;}
 
echo dirname(__FILE__).'';die;
}
**************************************************************************************************************************
 
function never entering in if(move_uploaded_file($uploadedFile, $url.__PS_BASE_URI__.'/img/'.$_FILES["fileUpload"]["name"]))  this condition , always going to else condition.
 
 
please let me know is there any issue in my code or how to resolve this issue.
 
waiting for your valuable reply
Edited by jafakash cp (see edit history)
Link to comment
Share on other sites

  • 6 years later...

public function fileUpload($uploadFile){

            $fileName=$_FILES['image']['name'];

            $temp_file = $_FILES['image']['tmp_name'];

            $image_path = dirname(__DIR__)."\sp_banner\images\\".$_FILES['image']['name'];

            if (move_uploaded_file($temp_file, $image_path)) {

                $output = $this->displayConfirmation($this->l(' File uploaded successfully'));

                return $fileName;

            } else {

                $output = $this->displayError($this->l('Error in uploading file'));

            }

        }

 

 

$insertData = array(

                    'name_slide' => $slidername,

                    'link' => $link,

                    'position' => $position,

                    'active' => $status,

                    'image' => $uploadFile,

                );

               

Link to comment
Share on other sites

Hi,

It's essential to sanitize the file name. Remove any characters that are not allowed or could cause issues in file names.

Instead of using backslashes (\) or forward slashes (/) directly in the file path, use DIRECTORY_SEPARATOR constant for better portability across different operating systems.

$fileName = preg_replace('/[^A-Za-z0-9_\-\.]/', '', $fileName); // Sanitize the file name
    
$image_path = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'sp_banner' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $fileName;

 

Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...