Php generated wrong format url file path after upload to server - php

Below is the code for me to generate a unique file Url of each image uploaded and store it to database.
//create unique image file name based on micro time and date
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = $now->format('YmdHisu');
// Path to move uploaded files
$target_path = "uploads";
//the file name
$path = "$target_path/$id.jpeg";
// getting server ip address
$server_ip = gethostbyname(gethostname());
// final file url that is being uploaded
$file_upload_url = 'http://'. $server_ip . '/' . 'abc' . '/' .$path;
What I expect for this code should generate the url in json_encode as below:
http://111.111.11.1/abc/uploads/20170226041004809200.jpeg
But now the problem is it generate the Url but in json_encode result is like below
http:\/\/111.111.11.1\/abc\/uploads\/20170226041004809200.jpeg
What I tried so far,
$file_upload_url = 'http:'. $server_ip . 'abc' .$path;
But it not what I want,it become this
http:111.111.11.1.1abcuploads\/20170226041515563600.jpeg
Can somebody have any idea how to solve this?Or have a better suggestion?

If you are working on same server then why don't you try to upload file with code snippet-
$file_upload_url = realpath($_SERVER["DOCUMENT_ROOT"]). '/abc' . '/' .$path;
// you can manage url by -
$base_url = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
$base_url.=$_SERVER['SERVER_ADDR'];
$base_url.='/abc/'.$path
// now print your url-
echo $base_url;

Use str_replace to replace the \
<?php
$file_upload_url = "http:\/\/111.111.11.1\/abc\/uploads\/20170226041004809200.jpeg";
$upload_path = str_replace('\\','',$file_upload_url);
echo $upload_path;
?>

Related

laravel file system returns string of data instead of video

i am trying to get a video from laravel file system like below :
$fileData = FileStorage::where('path', $name)->first();
$directory = $this->getDirectoryPathById($fileData->directory_id, false);
$path = Storage::disk('sftp')->get('/home/files/public/' . $directory . '/' . $fileData->path);
return $path;
but the path is not the .mp4 video that i except its a long string with unknown chars like imagebelow :

Store image in new directory in PHP

so i can upload my photo from my Android app fine to /var/www/html/ProductPhotos but when i want to get the name of the Product and use that as the name of the new directory and image name then its not working. I create the new directory and /var/www/html/ProductPhotos with 777 permissions (even though its super bad) but for now its what i need. here's my PHP code:
<?php
$ProductAccountName = $_POST['ProductAccountName'];
$ProductName = $_POST['ProductName'];
$ProductImage = $_POST['EncodedImage'];
$NewDirectory = "/var/www/html/ProductPhotos/" . $ProductAccountName;
mkdir($NewDirectory, 0777, true);
//$DecodedProductImage = base64_decode("$ProductImage");
//$ProductName = $ProductName .".JPG";
file_put_contents("/var/www/html/ProductPhotos/" . $ProductAccountName, $ProductName . ".JPG", $DecodedProductImage);
?>
You're using a comma instead of a period. And you're missing a slash.
file_put_contents("/var/www/html/ProductPhotos/" . $ProductAccountName . "/" . $ProductName . ".JPG", $DecodedProductImage);`
See the file_put_contents docs.
You may want to be put into place some checks to make sure the user doesn't use relative paths(using ../ as part the ProductAccountName, for example). Just be careful of the user using this to do malicious things.

Image Class Function not working correctly due to path

I'm using the below function:
class Image {
static function url($id, $type = 'maps') {
$path = UPLOAD_DIRECTORY . '/' . $type . '/';
$files = glob($path . $id . '.*');
if (count($files)) {
$ext = substr($files[0], strpos($files[0], '.') - strlen($files[5]));
return SERVER_URL . 'img/' . $type . '/' . $id . $ext;
} else {
return '';
}
}
}
It works fine when hosted on WAMP but when using on Unix running on nginx it doesn't display the image correctly because of the file path. It seems to add the physical path of the files in the URL so it thinks the patch of the file is http://localhost/demo/img/maps/20/public_html/demo/img/maps/20.png
On WAMP it displays correctly ie the URL is http://localhost/demo/img/maps/20.png
These are the defined variables:
define('SERVER_URL', 'http://localhost/demo/');
define('UPLOAD_DIRECTORY', dirname(__FILE__) . '/img');
id=20;
Physical location of the images are in /public_html/demo/img/maps/
How can I fix this on a unix OS.
Managed to work out a solution (btw I'm not a programmer).
Basically on the UNIX server the directory where the images were located had a dot in the path whereas on wamp it didn't.
So what I had to do is use the strrpost instead of strpost - this finds the position of the last occurrence of a substring in a string instead of the first occurance.

Get URL from server path

I am uploading a file using Codeigniter's File Uploading Library and trying to insert the URL into the database. Codeigniter only supplies the server_path when using $this->upload->data(), which isn't usable for displaying the image to users.
I could normally just do something like base_url('uploads) . '/' . $data['file_name'] but I am storing the images in a folder for each post.
For example, I am getting C:/xampp/htdocs/site/uploads/32/image.jpg as the full_path, how can I convert this to http://mysite.com/uploads/32/image.jpg
The only thing that comes to mind is using a regular expression, but I feel like there has to be PHP function or Codeigniter function to help with this?
How can I convert the server path into the correct URL?
You can use $_SERVER['HTTP_HOST']; to get the url. Using the ID for your post, you can construct your path like so:
$url = "http://" . $_SERVER['HTTP_HOST'] . "/" . $id . "/" . basename($data['file_name']);
the basename() function returns everything after the last / in your path.
you only need to save the filename to the database and just use the post id and filename:
$url = "http://mysite.com/uploads/" . $postID . "/" . $fileName;
to get the file name use: How to get file name from full path with PHP?
<?php
$path = "C:/xampp/htdocs/site/uploads/32/image.jpg";
$file = basename($path); // $file is set to "image.jpg"
$file = basename($path, ".jpg"); // $file is set to "image"
?>
In CodeIgniter you should use $config['base_url']
You can use the ENVIRONMENT to setup different base_url, for example:
switch(ENVIRONMENT)
{
case 'development':
$config['base_url'] = 'http://localhost/';
break;
case 'testing':
$config['base_url'] = 'http://testing.server.com/';
break;
default:
$config['base_url'] = 'http://liveserver.com/';
}
See config.php
Now simply replace your local path with the base_url, str_replace should do the job (documentation here)
$newpath = str_replace("C:/xampp/htdocs/site/", $config['base_url'], $localpath);
Also, if you're interested in getting parts of the path, you could use explode (documented here) with / to create an array with every "section" of your path
$pathElements = explode('/', $localpath);
In this case, $pathElements[0] is C:, $pathElements[1] is xampp, etc...

how can i change upload.php path with login user?

$upload_path = **'./uploads'/**; // The place the files will be uploaded to
i want change upload path(corrent: uploads) with login username.
Have you tried something like this?
$user = "admin"; //or whatever
$upload_path = "/uploads/" . $user;
I didn't really understand your question, but here is an example
$upload_path = './' . ($logged_in ? $username : 'uploads') . '/';

Categories