upload a file using FTP and php - php

I can't Upload the file using php because i can't send the path i got from the html file to the function FTP_PUT because it only takes string "test.txt"
How Can i send the Path to this function
PHP FILE
$file = $_POST["file"];
// upload file
if (ftp_put($ftp_conn, $file, $file, FTP_BINARY))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
HTML FILE
    
<div class="container">
 
          <div class="row">
            <div class="col-lg-12">
               <form class="well" action="Upload.php" method="post" >
                  <div class="form-group">
                    <label for="file">Select a file to upload</label>
                    <input type="file" name="file" id="file">
                   <!-- <p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p> -->
                  </div>
                  <input type="submit" class="btn btn-lg btn-primary" value="Upload">
                </form>
            </div>
          </div>
    </div>

Use $_FILES["file"]["tmp_name"] instead of $_POST["file"]
edit:
$file = $_FILES["file"]["tmp_name"];
$file_name = $_FILES["file"]["name"];
// upload file
if (ftp_put($ftp_conn, $file_name, $file, FTP_BINARY))
or move the uploaded file first:
$target_path = "uploads/".basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_path);

Change your form tag to :
<form class="well" action="Upload.php" method="post" enctype="multipart/form-data">
If you don't include
enctype="multipart/form-data"
Nothing will get uploaded!

Check that the path is correct... I don't know your file structure so I'm guessing you need the full path, try...
if (ftp_put($ftp_conn, getcwd().$file, $file, FTP_BINARY)

Related

Issue while File upload using ajax submit

In my index.php I have a form where a file upload will happen
<form id="uploadImage" method="post" action="upload.php">
<span class="input-group-text">
<label for="attach-doc" class="attachment-icon mb-0">
<i data-feather="image" class="cursor-pointer lighten-2 text-secondary"></i>
<input type="file" name="uploadFile" id="uploadFile" accept=".jpg, .png" /> </label></span>
</form>
I have a jquery for ajax submit as
$('#uploadFile').on('change', function(){
$('#uploadImage').ajaxSubmit({
// target: "#editor1 .ql-editor",
// resetForm: true
});
});
And my upload.php code is
<?php
//upload.php
if(!empty($_FILES))
{
if(is_uploaded_file($_FILES['uploadFile']['tmp_name']))
{
$ext = pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION);
$allow_ext = array('jpg', 'png');
if(in_array($ext, $allow_ext))
{
$_source_path = $_FILES['uploadFile']['tmp_name'];
$target_path = 'upload/' . $_FILES['uploadFile']['name'];
if(move_uploaded_file($_source_path, $target_path))
{
echo '<p><img src="'.$target_path.'" class="img-thumbnail" width="200" height="160" /></p><br />';
}
//echo $ext;
}
}
}
?>
File locations are :
So as per the code when I click on the upload file a ajax call should happen and the image should be uploaded to the folder upload .
But upload is not happening with the above code, Any help appreciated.
You have missed one form attribute enctype="multipart/form-data" that is used for file uploading.
If the form tag does not have this attribute, then your file will not be uploaded to the server.
Please change following line:
<form id="uploadImage" method="post" action="upload.php">
to
<form id="uploadImage" method="post" action="upload.php" enctype="multipart/form-data">
I hope this will work for you

How to create an upload form with upload and custom filename in php?

Hi there I'm trying to create an upload form in php. Containing, upload input, text input (name to be given to the file that was uploaded), and submit button. However I don't know much about php, so I don't know how to actually link that what I had typed on the <input type="text"/> becomes the name of the file when uploaded. If someone can help ? Thanks.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>Tu peux uploader ici ta video.</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="text" name="file-name"></input><br /> <!-- [ASK]: How to make this the file name of the uploaded file -->
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
​
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
move_uploaded_file ( string $filename , string $destination ) takes filename which is the filename of the uploaded file and destination is the destination of the moved file. Note that destination directory must exist; move_uploaded_file() will not automatically create it. So lets get the name now...
$_FILES['uploaded_file']['tmp_name'] gives you the temporary filename of the file in which the uploaded file was stored on the server eg. C:\Users\USER\AppData\Local\Temp\phpD3C.tmp
while
$_FILES['uploaded_file']['name'] gives you the actual name which you need to extract the extension eg. myfile.jpg
To link that what you had typed on the , first get it via $_POST["file-name"], then concatenate with the extension. Use pathinfo to retrieve the original extension.
Change your code to become...
<!DOCTYPE html>
<html>
<head>
<title>Tu peux uploader ici ta video.</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="text" name="file-name"></input><br /> <!-- [ASK]: How to make this the file name of the uploaded file -->
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(!empty($_FILES['uploaded_file']))
{
$file_parts = pathinfo(basename( $_FILES['uploaded_file']['name'])); //access the actual name instead of tmp_name
//just pick the extension of the file_parts and concatenate it to your path
$path = 'images/';
$path = $path . $_POST["file-name"] . "." . $file_parts['extension'] ;
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename($path) ." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
This should get you the desired name. So if you upload file dog.php and in the text field you have cow, the resulting name should be cow.php.
Result:

How to send file to $_FILE through html form?

Well pretty simple question.. But can't get it right for some reason.
What would be the html code to send a file to this?
move_uploaded_file($FILES["upload"]["tmpname"], $_POST["name"]);
Here's mine but when I used it and echo/vardump everything, I only have 'name' and not the file
<form action="uploader.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="upload" id="upload">
<input type="text" name="name" id="name">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Thank you
i try to add a comment but i can't
first check if upload permission is on in php.ini
file_uploads = On
If it set to on check your upload directory which you added in uploader.php file and use if to check $_FILES['upload'] is empty
this is a simple code to uploader.php file
<?php
if(!empty($_FILES['upload']))
{
$path = "upload/"; /// directory to upload
$path = $path . basename( $_FILES['upload']['name']);
if(move_uploaded_file($_FILES['upload']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['upload']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}else {
echo 'No file selected to upload ';
}

uploading files to server php

I tested my code locally and everything works fine, I tried with 0777 and 0755 permissions on server for the folders and still the upload of a file is not working. My code is:
$ds = DIRECTORY_SEPARATOR;
$storeFolder = '..\uploads';
if (!empty($_FILES)) {
foreach ($_FILES['file']['tmp_name'] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tempFile = $_FILES['file']['tmp_name'][$key]; //temporary file
$file_name = mt_rand(1000000,9999999); //generate the number for file name
$imageName = $file_name.'.' . pathinfo($_FILES['file']['name'][$key],PATHINFO_EXTENSION); // set the new file name
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; //target path where the image will be stored
$targetFile = $targetPath. $imageName; // set the target path and image, url
move_uploaded_file($tempFile,$targetFile); //move the uploaded file
echo 'BCK../uploads/'. $imageName;
}
}
}
I tried almost everything. And uploading is still not working. This .php script returns response like this:
BCK../uploads/5631131.jpg
So obviously there is a file, but it is not uploaded.
HTML code is:
<form action="action.php" enctype="multipart/form-data" class="dropzone" style="float:left; background-image: url(design.png); ">
<div class="fallback">
<input name="file" type="file" multiple />
</div>
</form>
Any help or advice would be appreciated since I now tried everything and still no result.
response i get local:
tempFile C:\xampp\tmp\php89FF.tmp
file_name 4337681
imageName 4337681.jpg
targetPath C:\xampp\htdocs\designer\pk\../uploads\
targetFile C:\xampp\htdocs\designer\pk\../uploads\4337681.jpg
BCK../uploads/4337681.jpg
response i get on server:
tempFile /tmp/phpjs6s9x
file_name 5394604
imageName 5394604.jpg
targetPath /var/www/designer/pk\..\uploads\
targetFile /var/www/designer/pk\..\uploads\5394604.jpg
BCK../uploads/5394604.jpg
try to change this line:
$storeFolder = '..\uploads';
to this:
$storeFolder = '..' . $ds . 'uploads';
I tested your code and had to add a few things to make it work, which will most likely work for you as well.
method="post" was missing in:
<form action="action.php" enctype="multipart/form-data" class="dropzone" style="float:left; background-image: url(design.png); ">
<div class="fallback">
<input name="file" type="file" multiple />
</div>
</form>
which is required when uploading files.
Also, missing square brackets for name="file" which should read as name="file[]" - It didn't work for me till I added it, because of the foreach argument.
foreach ($_FILES['file']['tmp_name'] as $key => $error)
The brackets are required when using a foreach and multiple files, so that it's treated as an array.
Your form should now read as:
<form action="action.php" method="post" enctype="multipart/form-data" class="dropzone" style="float:left; background-image: url(design.png); ">
<div class="fallback">
<input name="file[]" type="file" multiple />
<input type="submit" name="submit" value="Upload">
</div>
</form>
I added <input type="submit" name="submit" value="Upload"> since I didn't see it in your posted code.
You also could change $storeFolder = '..\uploads'; to $storeFolder = '../uploads'; with a / instead of \ but that might not make a difference, yet on some systems it does; I've seen it happen before.
You need to make create your target path folders if not created and consider not creating them if already exist. You need also to make sure that, the destination folder is writable. use "docroot" for referring to the root folder on your server. You can use something like this
if($_FILES )
{
$user_id=$_GET['user_id'];
$device_id=$_GET['device_id'];
$backup_id=$_GET['backup_id'];
$images_folder='images';
$target_path=DOCROOT.$user_id.'\\'.$device_id.'\\'.$backup_id.'\\'.$images_folder.'\\';
if ( ! is_dir($target_path) OR ! is_writable($target_path))
{
mkdir($target_path, 0777, TRUE);
}
$target_path = $target_path .$_FILES['uploadedfile']['size']. basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
$image_path=$target_path;
$image_size=$_FILES['uploadedfile']['size'];
$image = ORM::factory('image');
$image=$image->new_image($_GET['backup_id'],$image_path,$image_size,$_GET['mobile_path'],file_get_contents($_FILES['uploadedthumb']['tmp_name']));
echo "Server: Image: ".basename( $_FILES['uploadedfile']['name']).", uploaded successfully";
}
else
{
echo "Server: Error";
}
}
else
{
echo "no file attached";
}

PHP---why can't I upload image with PHP?

The code
<?php
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 5242888;
$upload_path = '/files';
$filename =$_FILES['userfile']['name'];
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1); //Get the extension form the filename.
if(!in_array($ext,$allowed_filetypes))
die('the file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['size'])>$max_filesize)
die('the file you attempted to upload is too large.');
if(!is_writable($upload_path)) {
die('you cannot upload to the specified directory,please CHMOD it to 777.');
}
if (move_uploaded_file( $_FILES['userfile']['tmp_name'],$upload_path.$filename))
{
echo 'you file upload successful.view the file here';
}
else{
echo 'failed';
}
When I upload a JPEG image, it shows the error "The file you attempted to upload is not allowed.". What's wrong with my code?
The main HTML code:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<button>upload</button>
Please use in form tag
<form action="upload.php" method="post" enctype="multipart/form-data">
Try replace:
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1);
//get the extension form the filename
by
$ext = substr($filename,strpos($filename,'.'),4);
//get the extension form the filename
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<input type="submit" name="sendFile" value="Upload" />
</form>
that should work.
Try with a submit button, <input type="submit " name="upload" value="upload"/>.

Categories