Unable to load a file from given path php - php

I am having problems in loading a jpg file path in my php script.I specified the file path correctly because when I enter it in the browser, it shows me the image in the browser.But
when I try to run the script it says
Unable to open 'http://localhost/php/testig/assets/Others/uploadify/memes/abc100003052455644.jpg' for writing: Invalid argument
I am returning this path to the client side. So if I use dirname(__ FILE __) this works fine but when I try to dynamically change the path of image on client side using jquery.It gives me error saying.
Not allowed to load local resource

$current_path = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
$file_path = $current_path . "somefolder/folder2/filename.jpg";
Try to catch your file path this way..
At your PHP code you should be working with real paths.
Edit:
$www_path = "http://localhost/folder/folder/folder/img.jpg";
The first $file_path you can use inside your PHP code (to open/edit/delete your image file inside your PHP code ONLY).
And the $www_path is the path which can be seen from your users and you may put as Image src at your HTML.

The issue is with your /php/testig/ - this should be /php/testing/. A simple typo!

Related

PHP Saving and outputting GD Lib image - relative and absolute paths

I have a php script which generates an image using GD Lib, saves it to a predefined location. And then should output it.
My directory structure is like this:
www.example.com/projects/project-1/
Inside the project-1 directory I have these directories:
- /imgs/
- /js/
- /css/
- /php/
The script using GD Lib is in /php/ with another config.php script where the constants are defined. This is then included in the main script.
Say I have two constants:
define('SAVE_PATH', '/projects/project-1/imgs/'); //The image will not save - this does not work
define('OUTPUT_PATH', '/projects/project-1/imgs/'); //this works if there is an image in this location
I then save the image like so:
imagejpeg($im, SAVE_PATH.$name, 100);
I get the following error:
failed to open stream: No such file or directory in /public_html/projects/project-1/php/main.php
Is it possible to do this with just one constant that works for both saving and outputting?
I know I can't have an absolute save path like: http://www.example.com/projects/project-1/imgs/
And I know I can't have an absolute output path like: /public_html/projects/project-1/imgs/
So what is the most elegant solution to this problem?
Your issue is likely due to using absolute pathing in your SAVE_PATH. An absolute path begins with /, and a relative path does not. These will probably work:
define('SAVE_PATH', '../imgs/'); //path relative to the php script, assuming the php file isn't being included from another path
define('SAVE_PATH', '/public_html/projects/project-1/imgs/');
BUT
In efforts to make this more versatile, I would instead do the following. Set 2 constants, one for the base application directory, and one for the path to the image folder. The latter will be usable in conjunction with the former for the save path, and will be usable on its own as the output path:
//const instead of define is a little prettier, but this is a preference
const APPLICATION_PATH = '/projects/project-1';
const IMAGE_PATH = '/imgs/';
//Save the image - Absolute path to the file location
imagejpeg($im, APPLICATION_PATH.IMAGE_PATH.$name, 100);
//Echo the image url - Absolute path to the file url
echo "<img src='".IMAGE_PATH.urlencode($name)."' />";
You now only need to edit IMAGE_PATH to make changes to where the images reside which will appropriately affect both the system directory and the web url.

PHP move_uploaded_file is Not uploading Images when full web path is given in Unix system

My Following code is working fine when I set
$target = "size_images/14_20131216231522_cashew.jpg";
But Not Working in Actual php Code when full web address path is given though 777 Unix access is granted to system.
I am trying upload an image from SubDomain to Main Domain, So i need to give Full Path.
Page: http://subdomain.examples.com/
Code:
$target = "http://examples.com/size_images/14_20131216231522_cashew.jpg";
move_uploaded_file($_FILES["item_image"]["tmp_name"],$target);
For your reference, following are values of above code line...
echo $_FILES["item_image"]["tmp_name"]; --> "/tmp/php6RNC28"
echo $target --> "http://examples.com/size_images/14_20131216231522_cashew.jpg"
Even tried with relative path instead of http, No luck : /home/direc/www/size_images
No use of placing error code. its not returning any error.
error_reporting(E_ERROR | E_PARSE);
Try this, You need to use only relative path instead of domain path. Domain path doesn't work
$target = "size_images/14_20131216231522_cashew.jpg";
move_uploaded_file($_FILES["item_image"]["tmp_name"],$target);
instead of ,
$target = "http://examples.com/size_images/14_20131216231522_cashew.jpg";
move_uploaded_file($_FILES["item_image"]["tmp_name"],$target);
Send a file via POST with cURL and PHP Ref: http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
Try using:
$target = $_SERVER['DOCUMENT_ROOT']."/size_images/14_20131216231522_cashew.jpg";
If you want to upload from subdomain.mydomain.com to mydomain.com simply put the upload script on mydomain.com and then use a relative path.
You are misunderstanding how move-uploaded-file() works. It is similar to "File Save", namely you have to tell the PHP script a locally-writeable directory and file name where the file goes.
You mention you're trying to go from "subdomain" to "main domain"... if these two web urls are hosted on the same machine, this will be possible, you will just choose the directory that has the files for the "main domain" site.
It has to be a relative path.. absolute path would not work.
Also make sure that you know the difference between "/" and "./" But I would strongly recommend to either use configuration variables(in case of framework) OR constants to store file path.
Former point to root and later point to current directory..

CodeIgniter download a file to windows via FTP

In my website I have the following 2 functions. The first one takes the FTP connection and creates a directory in the specified path. server.com/Host_path/DirectoryName for example. This piece of code works perfectly fine. I have an upload function that uploads file test.txt to the folder DirectoryName and the second function is supposed to download that file to my local Windows machine. However i get the error message Unable to download the specified file. Please check your path. With no indication as to which path is wrong.
$path = 'server.com/' . $result['path'];
$this->ftp->mkdir($path);
When I echo the remote and local paths I get remote = server.com/Host_path/DirectoryName/test.txt and local = C:\Users\Owner\Desktop\test.txt . Based on the fact that the above function works but the below function does not, I believe that the Windows path is wrong but it could be a difference in functions.
$remotePath = 'server.com/' . $result['path'];
$localPath = 'C:\\Users\\Owner\\Desktop\\test.txt';
$this->ftp->download($localPath, $remotePath, 'auto');`
Please Help!
For remotePath, use absolute path starting with "/"

How to upload a file using php to the parent directory

Hopefully easy question, I have a desktop application that allows the user to upload a file to a server using a form, the form sends the data to a protected file on the site like this. Site_root/protected_folder/myfile.php . If you use php file upload commands normally you'd be operating in the 'protected_folder' directory, which I don't want.
I want to add stuff to the images file on the root directory like this Site_root/images/ , how would you go about doing this without going the ftp root?
The usual method is to call move_uploaded_file(), where you set the destination path to your liking. The file name in $_FILES['tmp_name'] normally points to a temporary folder and it's subject to be removed without prior notice.
You can either use an absolute path like /path/to/images/ or use a relative path like ../images/
Assuming you're using move_uploaded_file the second paramater takes the directory that you wish to upload to. Perhaps showing you code may help if this post doesn't.
move_uploaded_file() will allow you to place uploads relative to the root directory if you simply start your path with a slash like
$newFileDir = '/username/public_html/websiteroot/subdir/yourfile.jpg';
move_uploaded_file($_FILES['postname']['tmp_name'],$newfileDir);
you can simply use copy() and double dot (../) in path to specify root directory to copy the uploaded file. I'm using the same. You may want to change the file name so that there will be unique filename in the directory error will occur. extension will also be same.
//
$filename = stripslashes($_FILES['postname']['name']);
$extension = getExtension($filename);
$newfilename ='photo_your_filename'.time().'.'.$extension;
$newFileDir = '../subdir/'.$newfilename;
copy($_FILES['postname']['tmp_name'],$newfileDir);

PHP Image Upload trouble

I am currently working on a Joomla! website. I am using the Jumi extension (which allows custom scripts to be made and executed within Joomla! itself) to create a simple file upload tool. The problem is that I get the following error:
Warning: copy(C:/xampp/htdocs/images/1253889508.jpg) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\Joomla\components\com_jumi\jumi.php(25) : eval()'d code on line 61
The offending code is as follows:
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname='C:/xampp/htdocs/images/'.$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
The full code is available here. Once I get this working, I will be modifying it to meet the needs of my website.
I'm not sure if the problem is a permissions issue with Jumi or if there is some other problem. The best I can tell is that for whatever reason, the temp file is not being created.
Thanks for any tips you may have!
Try this:
if(move_uploaded_file($_FILES['image']['tmp_name'], $newname)){
// move worked, carry on
}
And use relative paths instead of absolute ones.
does your C:/xampp/htdocs/images directory actually exists?
if not create it manually or with mkdir()
also try to use the constant DIRECTORY_SEPARATOR instead of hardcoding slashes
Also you should use the move_uploaded_file() for this and not the copy() function.
And never hardcode absolute paths into your scripts! instead get the root path and preferibly set it as a constant, this is mostly done with the dirname() function in the entry file, but joomla allready has a constant you can use for this.
Forward slashes in the $namename path rather than backslashes?

Categories