Can't upload file (random tmp path) - php

I'm using lampp 7.2.5 & also tried on server with php 7.1. I'm trying to create a file hosting website but there is only 1 error. I couldn't find any solution for it so I'm asking for help now. I have a PHP code like this:
$ftp_server = xxxxxxxxxxxxxx;
$ftp_username = xxxxxxxxxxxxxxxxxxxxxxx;
$ftp_userpass = xxxxxxxxx;
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = $_FILES['file']['tmp_name'];
$file_name = $_FILES['file']['name'];
$remote_file = "/uploaded/".$file_name;
if (ftp_put($ftp_conn, $remote_file, $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
ftp_close($ftp_conn);
And HTML:
<form action="sent.php" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file" class="inputfile" onchange="danee()">
<label for="file" class="chose" id="chose">Wybierz plik</label>
<div id="info">
<input type="email" name="email" id="email" onchange="checkEmail()" autocomplete="email" class="userdata" placeholder="E-mail"><Br/>
<input type="password" name="pass" id="pass" onchange="checkPass()" autocomplete="new-password" class="userdata" placeholder="Hasło do pliku"><Br/>
<input type="submit" value="Wyślij" id="send">
</div>
</form>
And the problem is when I reload the browser and try to send any file, there shows that error:
Warning: ftp_put(): Can't open that file: No such file or directory in /opt/lampp/htdocs/host/sent.php on line 10
Error uploading /opt/lampp/temp/phpgQoyn9.
And there always shows "/opt/lampp/temp/php[and some random characters]
Can someone help me? Pleaseee

You are upload image in directory. And then execute other process.
move_uploaded_file($file, $remote_file)
if (move_uploaded_file($file, $remote_file) && ftp_put($ftp_conn, $remote_file, $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
ftp_close($ftp_conn);

Related

How to send file from local computer to my server in php?

I want to upload one image file(example - "C:\Users\Public\Pictures\1.png") to my ftp server(ftp://srict.96.lt).
I have the following HTML code which allows user to select the file from local computer.
<html>
<body>
<form action="imgup.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
now in the imgup.php I tried the following code but it says "move_uploaded_file(/Hydrangeas.jpg): failed to open stream: Permission denied in /home/u704250527/public_html/imgup.php on line 21"
<?php
$target_dir = "public_html/"; /* I also tried for "/" "root/public_html/" */
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if ($uploadOk == 0)
{
echo "Sorry, your file was not uploaded.";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
?>
I need the file to be uploaded when user clicks upload button.
I also tried using ftp in php this also gives me the same error failed to open stream.
<?php
// connect and login to FTP server
$ftp_server = "ftp.srict.96.lt";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username="user";
$ftp_userpass="pass";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "D:\Picture1.png";
$fp = fopen($file,"r");
if (ftp_fput($ftp_conn, "1.png", $fp, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
ftp_close($ftp_conn);
?>
I finally found what is wrong. The file which is uploaded has to selected and should be added using $_FILES['filetoupload']['tmp_name'];
This is the complete code below.
for HTML:
<!DOCTYPE html>
<html>
<body>
<form action="imgup.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Once user clicks uploads the following php script will execute and magic will happen.
<?php
// connect and login to FTP server
$ftp_server = "ftp.my.server.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username="my_username";
$ftp_userpass="my_password";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
var_dump($_FILES);
if (ftp_put($ftp_conn, "1.png",$_FILES['fileToUpload']['tmp_name'], FTP_BINARY))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
This is the important line here This says to upload file using FTP_BINARY mode which is mostly preferred for image and document files. Also note "tmp_name" to upload.
ftp_put($ftp_conn, "1.png",$_FILES['fileToUpload']['tmp_name'], FTP_BINARY)

Upload a file to ftp server by php

I am trying to upload a photo to my ftp server. Here is te html code:
<html>
<body>
<form enctype="multipart/form-data" action="db.php" method="POST">
Choose a file to upload:
<input name="file" type="file" />
<br/>
<input type="submit" value="Upload File" />
</form>
</body>
</html>
And it is my php code:
<?php
$ftp_server="myserver";
$ftp_user_name="username";
$ftp_user_pass="pass";
$file = $_FILES['file']['name'];
$remote_file = "x.png";
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$upload = ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
if(move_uploaded_file ($file , "/photos")) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
ftp_close($conn_id);
?>
but when i trying to upload a file it show an error like:
Warning : ftp_put(o.png) [function.ftp-put ]: failed to open stream: No such file or directory in /home/ a1268559/public_html/ profile/db.php
Please help me
You have to do move_uploaded_file first before ftp_put
also move_uploaded_should be like this
move_uploaded_file ($file , "/photos/$filename");
then your ftp_put would be like
$upload = ftp_put($conn_id, $remote_file, "/photos/$filename", FTP_ASCII);

file uploading using ftp_put [duplicate]

This question already has an answer here:
PHP Warning: ftp_fput(): Can't open that file: Is a directory in
(1 answer)
Closed 2 years ago.
I have solved my original question below. What is the best method if I need to insert a progress bar for the file uploader? Using the ftp_put method, doesn't allow to get a response from the server until the file is successfully uploaded.
Thanks!
I'm trying to upload a file using ftp_put and for some reason when I run the code through localhost, it gives me the below error:
Warning: ftp_put(): Can't open that file: Is a directory in C:\xampp\htdocs\ftp\upload_file.php on line 25
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<input name="uploadedfile" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
PHP Code:
<?php
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = "";
$destination_file = "/public_html/testing/";
$source_file = $_FILES['uploadedfile']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
Thank you.
Please test chdir that destination or look at this on php.net
Got this cryptic error
Warning: ftp_put() [function.ftp-put]: 'STOR' not understood in
C:\wamp\www\kevtest\ftp_todays.php on line 48
Found the prob, you can't put a path to the destination file
(even though I can do that in the dos ftp client...?)
e.g. - this doesn't work
ftp_put($conn, '/www/site/file.html','c:/wamp/www/site/file.html',FTP_BINARY);
you have to put
<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );
?>
<?php
if(isset($_POST['submit']))
{
ini_set('max_execution_time', 60000);
$file = $_FILES['video']['tmp_name'];
$destination_file = "/public_html/".time().basename($_FILES['video']['name']);
// connect and login to FTP server
$ftp_server = ".com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, 'name', 'pass');
// upload file
if (ftp_put($ftp_conn, $destination_file, $file, FTP_BINARY))
{ echo "Successfully uploaded ".$_FILES['video']['name']." "; }
else
{ echo "Error uploading $file."; }
// close connection
ftp_close($ftp_conn);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<body>
<div class="container">
<form name="frm" id="frm" method="post" enctype="multipart/form-data">
<div class="input-group">
<input type="file" name="video" required="required" />
</div>
<div class="input-group">
<input type="submit" name="submit" value="Submit" onClick="move()"/>
</div>
</form>
</div>
</body>
</html>

PHP - Upload images to FTP directory and then Display images uploaded only from directory - 2

While trying to upload image and display it, i'm getting the 'Failed moving the uploaded file'. Don't know where I'm going wrong..
Here's my image.php:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile_1" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Here's my upload.php:
<?php
$ftp_server = "localhost";
$ftp_username = "xxx";
$ftp_password = "xxx";
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
if(#ftp_login($conn_id, $ftp_username, $ftp_password))
{
echo "connected as $ftp_username#$ftp_server\n";
}
else {
echo "could not connect as $ftp_username\n";
}
$remote_file_path = "/2013/img/" . $_FILES['uploadedfile_1']['name'];
if ( $_FILES['uploadedfile_1']['error'] )
die( "Upload failed with error code " . $_FILES['uploadedfile_1']['error'] );
$new_path = '/2013/img/tmp/' . $_FILES['uploadedfile_1']['name'];
if ( ! move_uploaded_file( $_FILES['uploadedfile_1']['tmp_name'], $new_path ) ) {
die( "Failed moving the uploaded file" );
}
ftp_put($conn_id, $remote_file_path, $new_path,FTP_BINARY);
echo "<img src='$new_path' alt='Your uploaded file' />";
?>
Any help would be much appreciated.
There is a problem with your folder where you try to put the image
try this see if it works
if ( ! move_uploaded_file( $_FILES['uploadedfile_1']['tmp_name'], 'D:/'.$_FILES['uploadedfile_1']['name']

FTP upload via PHP form

I want to upload a file via FTP upload in a form.
<html>
<body>
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
Here is the PHP file:
<?php
$ftp_server = "xxx";
$ftp_username = "xxx";
$ftp_password = "xxx";
// setup of connection
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
// login
if (#ftp_login($conn_id, $ftp_username, $ftp_password))
{
echo "conectd as $ftp_username#$ftp_server\n";
}
else
{
echo "could not connect as $ftp_username\n";
}
$file = $_FILES["file"]["name"];
$remote_file_path = "/home/www/lifestyle69/import/".$file;
ftp_put($conn_id, $remote_file_path, $file, FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>
The FTP connection connects successfully but the file is nowhere.
Can anybody help me?
Thanks!
Because you have <input name="uploadedfile" type="file" />:
$file = $_FILES["file"]["name"]; // wrong
$file = $_FILES["uploadedfile"]["name"]; // right
Because you need the filename of the temporary copy stored by PHP, which exists on the server:
ftp_put($conn_id, $remote_file_path, $file, FTP_ASCII); // wrong
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile"]["tmp_name"],
FTP_ASCII); // right
Refer to the PHP documentation for more information about $_FILES.
Are you sure that the folder you are uploading to has the correct permissions? Try chmoding it to 777 and see if that works.
The file is stored on server with temporary name, so when you try uploading $_FILES['file']['name'], it fails, because file with such name does not exist. Instead you should call ftp_put() with $_FILES['file']['tmp_name']
It's explained a little better here

Categories