CodeIgniter download a file to windows via FTP - php

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 "/"

Related

Create variable that contains working path for including files on both localhost and live server (PHP)

I am working on a project with a lot of includes and requires. I don't want to change this everytime I deploy from localhost to my live server.
So I thought to use $_SERVER['DOCUMENT_ROOT'], which worked fine until I checked my git which pushes directly to a backup folder on my live server. It is not in the root, but one level lower.
All the files in this backup folder that use include or require get their files from my server root (public_html/) not the current folder the project is in (public_html/backup/).
So this wouldn't work. After that I tried getcwd();, this fixed above situation but now I can't include files on my localhost.
So now I thought to make a check to see if the file is currently on my local pc or on my live server like this:
$checkpath = getcwd();
$localcheck = 'C:\\';
// Test if path contains C:\
if(strpos($checkpath, $localcheck) !== false){
$absolute_path = $_SERVER['DOCUMENT_ROOT'];
} else{
$absolute_path = getcwd();
}
My only problem is how can I include this for all files when including is my issue?
Maybe there is a better way to get a path that always works no matter if it is live or local?
You can use set_include_path($absolute_path); to change the directory include/require looks from. You would then technically only need to call this once in every PHP request. Beware that you need to provide the actual path to set_include_path. For example, you would need to provide "/home/username/directory" instead of "~/directory".
set_include_path documentation.

Image directory location in Remote server

I am new to php.
I am using below code to upload image in local folder path "uploads/profilepics/".
move_uploaded_file
(
$_FILES["file"]["tmp_name"],
"uploads/profilepics/".$filenameWithUsername
);
It is working fine.
In remote server, my folder structure look like this, /public_html/myfamily/uploads/profilepics
I would like to know a good approach to store the image in remote server, if the code need some improvement?
I would use the __DIR__ magic constant (if youre lower than php 5.3 dirname(__FILE__)) to make sure your path is the same on local as on your server.
This will give you a fullpath to your dir no matter on what machine you are running.
So in your case id say try:
move_uploaded_file(
$_FILES["file"]["tmp_name"],
__DIR__ . "/uploads/profilepics/".$filenameWithUsername
);
if it doesn't work
echo __DIR__ . "/uploads/profilepics/".$filenameWithUsername;
See what it shows and edit the path to where it needs to go if it isn't correct

unlink PHP works when file is in root, not if file is in folder

So this one is pretty straight forward I want to delete a file on the server using PHP, I have:
$myfile = 'theone.png';
unlink($myfile);
This code deletes the file, howevere if the path to file is /images/theone.png, it doesn't work, I have tried images\theone.png with no luck.
If I try and connect with FTP I get the error message to say that cURL does not support the unlink function... Any help would be great.
Thanks Guys!
What about:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
$myfile = '$root/images/theone.png';
unlink($myfile);
Although to my knowledge, your attempted method should work, unless either I'm missing something, or you haven't included some code here that might be interfering with the unlink.
__DIR__ - this magic constant contains current directory, in case that the file is in the same directory as your PHP script you can use:
unlink(__DIR__ . "/$myfile");
If the file is for example in one directory above your PHP script you can use:
unlink(__DIR__ . "/../$myfile");
If the directory has correct access rights it should work.

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..

Unable to load a file from given path 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!

Categories