I've been trying to make a system that can upload large files, originally i used HTTP but this had a number of problems with settings that needed to be changed. So i thought i'd give it a go with FTP.
Now i have a ftp connection in PHP and it works find, i can view folders and files as well as make directories, what i can't seem to figure out though is how to get hold of a local file and upload it.
I have been reading lots of information and tutorials such as the PHP manual and a tutorial i found on nettuts But i'm struggling to do it. The tutorial says you can upload a local file but i must be missing something.
Here is the upload method i'm using:
public function uploadFile ($fileFrom, $fileTo)
{
// *** Set the transfer mode
$asciiArray = array('txt', 'csv');
$extension = end(explode('.', $fileFrom));
if (in_array($extension, $asciiArray))
$mode = FTP_ASCII;
else
$mode = FTP_BINARY;
// *** Upload the file
$upload = ftp_put($this->connectionId, $fileTo, $fileFrom, $mode);
// *** Check upload status
if (!$upload) {
$this->logMessage('FTP upload has failed!');
return false;
} else {
$this->logMessage('Uploaded "' . $fileFrom . '" as "' . $fileTo);
return true;
}
}
When trying to upload a file i use this:
$fileFrom = 'c:\test_pic.jpg';
$fileTo = $dir . '/test_pic.jpg';
$ftpObj -> uploadFile($fileFrom, $fileTo);
I thought this would get the file from my machine that is stored in the c: and upload it to the destination but it fails (Don't know why). So i changed it a little, changed the $fileFrom = test_pic.jpg and up the picture in the same folder on the remote server. When i ran this code the script copied the file from the one location to the other.
So how would i go about getting the file from my local machine to be sent up to the server?
Thanks in advance.
Using this you would upload a file from your PHP server to your FTP server, what actually not seems to be your target.
Create an upload form which submits to this PHP file. Store this file temporarily on your server and then upload it to your FTP server from there.
If your try would actually work, this would be a major security issue, because a PHP file would have access to any files on my local machine.
Related
I'm developing a web application with PHP, to upload files to a web server which merges, compresses those files (pdf) and then upload the new files to another FTP server. This second ftp server is used as a network driver in my offices so people can work with uploaded files.
The number of files goes from 2 to 7 for one upload.
I'm having the following problem: Sometimes files are uploaded, but most of the time they aren't. Sometimes one file on three is uploaded and the two others are not.
I almost always get this:
Warning: ftp_nb_put(): The connection timed out in ...
Warning: ftp_nb_put(): Opening data channel for uploading files to server from...
Warning: ftp_nb_put(): File not found in...
I tested with another (personal) FTP server from a webhost and it worked perfectly 10/10 times.
So I guess that the problem is coming from the ftp server.
I also tested with passive mode, but I get more error, even with :
ftp_pasv($ftp, true);
ftp_set_option($ftpconn, FTP_USEPASVADDRESS, false);
I don't have access to the FTP server settings, so I would like to know what could possibly be blocking downloads to give information to the service that manages this.
This is my code, for the FTP part :
$ftp = ftp_connect($ftp_hostname, 21);
ftp_login($ftp, $ftp_username, $ftp_password);
ftp_mkdir($ftp, $targetdir);
foreach($file as $filename => $path) {
if(file_exists($path)) {
$upload = ftp_nb_put($ftp, $targetdir.$filename, $path, FTP_BINARY, FTP_AUTORESUME);
while (FTP_MOREDATA == $upload){
$upload = ftp_nb_continue($ftp);
}
if ($upload != FTP_FINISHED) {
echo "Error with : ".$path;
}
}
else {
echo("File not found : ".$path);
}
}
ftp_close($ftp);
I hope I didn't miss any information,
Thanks !
I use this script to upload a file to the server using laravel.
$fileName = time().'_'.$request->file->getClientOriginalName();
Storage::put('/public/uploads/'.$fileName,$request->file('file'));
$url = Storage::url('public/uploads/'.$fileName);
$fileModel = new File;
$fileModel->name = time().'_'.$request->file->getClientOriginalName();
$fileModel->file_path = $url;
$fileModel->save();
this code works great in local host (wamp server) but when I use it in the server, get this error:
"message": "The "" file does not exist or is not readable.",
"exception": "Symfony\Component\Mime\Exception\InvalidArgumentException",
I also tried to upload lots of know file types (png,pdf,jpeg,mp4,...). same error
Check for folder permission issues including the temporary folder on the server tmp.
Check the maximum upload file size: upload_max_filesize and post_max_size in your server settings.
I have a web application which is connected to a website. my web application runs in a Ubuntu server(LAMP). in my web application, there is a function called add a new client. when adding a new client user have to set an image for the client. when a user clicks the save button, the image should upload to the Linux server itself and I'm sending the image to my shared hosting server where the website is hosted using FTP. This perfectly works in my windows localhost (I'm using wamp server). but when I put my web application to the Ubuntu server, FTP image upload works sometimes and sometimes it doesn't work.
this is my code
private $config_ftp = array('hostname'=>'xxxx.com','username'=>'xxx#xxxxx.com','password'=>'xxxxx','passive'=>FALSE,'debug'=>FALSE,'port'=>21);
$source_original = $this->img_path['img_original'] . $file_name;
$destination_original = $this->full_file_path['img_original'] . $file_name;
if($this->ftp->upload($source_original,"./assets".$destination_original)) {
$this->response['msg']="Images Uploaded successfully!";
$this->response['status']=true;
}
when I check the destination folder after the upload it looks like this. The file size is 0kb. sometimes it gets uploaded properly. and I have to mention again this process works completely fine in windows localhost.
Thanks.
$this->ftp->upload() code is need to be shown to find actual problem.
in that if $image_size = $_FILES['image']['size']; $image_size is 0 that you have problem in image otherwise you can successfully upload it by move_uploaded_file($_FILES['image']['tmp_name'], "YOUR-UPLOAD-FOLDER/YOUR-IMAGE-NAME-PLUS-EXTENSION"); .
similar problem : 0 kb when upload and store image to server
The issue was there in my ftp_config.
what i had to do is set passive and debug as true
private $config_ftp = array('hostname'=>'xxxx.com','username'=>'xxx#xxxxx.com','password'=>'xxxxx','passive'=>TRUE,'debug'=>TRUE ,'port'=>21);
The following code works perfect on my localhost but it shows the following errors on my live server
Warning: move_uploaded_file(.../uploads/76948893.jpeg): failed to open stream: No such file or directory
Warning: move_uploaded_file(): Unable to move '/tmp/phppxvRs8' to '.../uploads/76948893.jpeg'
What it does is simple, it takes the images on the array ["pictures"] which comes from a html form and save every image on the folder ".../uploads/" using a random numeric name as name of the file and keeping the original extension.
Any one knows how to make it work on my server?
//Image Uploader
$images=[];
$directory = '.../uploads/';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
$new_file_name = rand (10000000,99999999);
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
/* echo '<br>';
echo $directory.$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1);*/
if(move_uploaded_file($_FILES['pictures']['tmp_name'][$key],
$directory
.$new_file_name
.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1))) {
array_push($images,$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1));
$images_validator=true;
}else{
//Error
}
}
}
There could be many reason for this, Check the following
You need WRITE Permission for the uploads directory. I assume your local machine runs windows & your hosting environment is linux
Like #Darren suggests, use absolute path. change the $directory to $directory = getcwd() . 'uploads/';
If this runs on your local machine but not on server there are 2 easy answers I can think off right off the back. 1. The folder doesnt exist on the server, 2. as someone comment you dont have permission to write/read to that folder on the server....I would check the server configuration to make sure your app pool or users have read/write permissions to the folder
So here's the story, im not very experienced with php, and recently moved to a new host and my website worked prefectly on the old host but on the new host there are errors with the code.
The error message says :
Warning: copy() [function.copy]: Unable to access http://i.imgur.com/USlH6p2.jpg in (directory)
Heres the code where it says there is an error
function upload_image_remote($image, $name) {
$upload_dir = APP_PATH . '/image.uploads';
//check for directory rights
if(!is_writable($upload_dir)) {
echo do_error(_('Folder image.uploads is not writeable'));
exit;
}
//check if there's a directory for today uploads
$today = date("d-m-Y");
if(!is_dir($upload_dir .'/' . $today)) {
if(!mkdir($upload_dir .'/' . $today, 0777)) {
echo do_error(_(sprintf('Folder <strong>image.uploads/%s</strong> could not be created. Please check permissions to be 0777.', $today)));
exit;
}
}
$upload_path = $upload_dir .'/' . $today;
(--> this is where) return copy($image, $upload_path .'/'. $name);
}
I think it may be a permissions problem since it worked perfectly on (old host) 000webhost, anyone have any ideas on what can be wrong with the code ?
Thanks
My guess would be that the copy function is trying to access the remote file and cannot due to the PHP settings.
I think most hosting companies, for security reasons, will have allow_url_fopen = 0. This means that you will not be able to read from the remote location (http://www.site.com/foo.gif). However, you will be able to read from the local file system.
From the PHP documentation:
This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.
To check this, view the current PHP settings on the server by creating a file with the following contents in your web root. Your looking for the allow_url_fopen setting.
<?php
echo phpinfo();
?>
Give folder permission 0755 Set correct path of temp folder for file
system.