Summernote and absolute path in the img-upload.php file

I’m using summernote as the text editor in a backend. The text and images stored must then be displayed in the pages of the frontend. The editor and the upload images works, but the problem is to recover the images in the frontend because of the path. I need to use the absolute path in the img-upload.php file but it doesn’t seem to accept it.

img-upload.php

if(empty($_FILES['file']))
{
exit(); 
}
$errorImgFile = "./img/img_upload_error.jpg";
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
$destinationFilePath = '../../images/img-uploads/'.$newfilename ;
if(!move_uploaded_file($_FILES['file']['tmp_name'], $destinationFilePath)){
echo $errorImgFile;
}
else{
echo $destinationFilePath;
}

with the relative path works:

$destinationFilePath = '../../images/img-uploads/'.$newfilename ;

but in this way not:

$path = 'http://localhost/sites/my-site/';
$destinationFilePath = $path.'images/img-uploads/'.$newfilename ;

Can anyone give me an explanation?
Thanks

You can’t just put files anywhere over HTTP. Use the filesystem absolute path.

Can you show your project structure please.
because this ‘…/…/images/img-uploads/’.$newfilename
doesn’t look like this one http://localhost/sites/my-site/images/img-uploads/’.$newfilename

Project structure where is working summernote:

http://localhost/sites/my-site/cpanel/product/

where i store the summernote images:

http://localhost/sites/my-site/images/img-uploads

$destinationFilePath = '../../images/img-uploads/'.$newfilename ;

The images are correctly stored into the img-uploads folder and into the database i have

<img src="../../images/img-uploads/1566394600.jpg"

Now, the problem is to recover the images into the page frontend side, because the path is relative
frontend structure:
http://localhost/sites/my-site/page.php
What i need is to use an absolute path and that’s not working as i wrote above.
What can i do? Thanks

First store only the image in your database or store the image with path not like this
<img src="…/…/images/img-uploads/1566394600.jpg"
but like this …/…/images/img-uploads/1566394600.jpg or like this

$image_path = /images/img-uploads/1566394600.jpg
Then create a constant with absolute path like define('APPROOT',__DIR__);
now retrieve image like this
 <img src="<?php echo APPROOT; ?>/<?php echo $image_path; ?>"> 
or 
$destinationFilePath = __DIR__.'/images/img-uploads/'.$newfilename ;

i hope it helps
if you are using php <= 5.3
try this 
define('APPROOT', dirname(__FILE__));

I can’t use your first solution because summernote use its own construct within the textarea. I am not able to change this.
I really liked the idea of using DIR

$destinationFilePath = __DIR__.'/images/img-uploads/'.$newfilename ;

but unfortunately doesn’t work.
The upload does not work and I don’t receive any error.
It does not seem to recognize any type of variable apart from the relative path written by hand
Did you tried it? Am I the only one with a similar problem?

No and no you are not the only one with this problem so please check this links out to see if you can figure out the relative path, it is quiet challenging but i think it could work

just try these links
and keep trying to figure it out it will work

Sponsor our Newsletter | Privacy Policy | Terms of Service