I have tried files upload to server using ftp connection in php and its not working, its connecting and and telling uploaded successfully but no image will be upload in the directories....i have tried following code please help by correcting it
image.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome</title>
</head>
<body>
<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 />
Choose a file to upload: <input name="uploadedfile_2" type="file" /><br />
<input type="submit" value="Upload Files" />
</form>
</body>
</html>
upload.php
<?php
$ftp_server = "XXXXXX";
$ftp_username = "XXXXX";
$ftp_password = "XXXX";
$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";
}
$file = $_FILES["uploadedfile_1"]["name"];
$file2 = $_FILES["uploadedfile_2"]["name"];
$remote_file_path = "/imagetest/123/".$file;
$remote_file_path2 = "/imagetest/123/".$file2;
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile_1"]["tmp_name"],FTP_ASCII);
ftp_put($conn_id, $remote_file_path2, $_FILES["uploadedfile_2"]["tmp_name"],FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>
try..
$remote_file_path = "./imagetest/123/".$file;
or simply...
$remote_file_path = "imagetest/123/".$file;
try this...
$uploaddir = "./temp/";
$uploadfile = $uploaddir . basename($_FILES['uploadedfile_1']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['uploadedfile_1']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
First, make sure paths of the files are fine (syntactically and are actually pointing to the file initended) and secondly, the ftp_put function returns a boolean. You could check it's value to see if the upload is happening successfully or not.
I think another problem in this case might also be that the server's firewall is hindering establishing a data channel between the client (i.e you) and the server. Try turning the passive mode on before uploading files. Just add the following line before ftp_put and see if it works:
// turn passive mode on
ftp_pasv($conn_id, true);
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
You will also have to use Binary mode for images as rightly pointed out.
Relevant documentation. Hope it gets you started in the right direction.
$_FILES["uploadedfile_1"]["tmp_name"] is just a string with temporary file name(such as 'aaaaa.jpg' but not '/tmp/upload/aaa.jpg'). You should use move_uploaded_file to move files uploaded with a form since $_FILES["uploadedfile_1"]["tmp_name"] doesn't contain the actual file path.
http://php.net/manual/en/function.move-uploaded-file.php
Since I haven't experience to move uploaded file directly to another server, here's what will work, I suppose.
1.First move the uploaded file to a local folder.
$localPath = '/xxxx/upload/' //confirm your web server user have the right to write in this folder
$tmp_name = $_FILES["uploadedfile_1"]["tmp_name"] // confirm you got a filename here.
move_uploaded_file($tmp_name, "$localPath/$tmp_name"); // You should have a new file in /xxxx/upload/
in your webserver.
2.Then move the file from local folder to remote server.
ftp_put($conn_id, $remote_file_path, "$localPath/$tmp_name" ,FTP_ASCII);// If this goes wrong, you should test this with a local file created mannually and upload it to your remote server. If it fails, there must be something wrong in your remote connection or permission.
3.Then delete the file in local folder.
unlink("$localPath/$tmp_name");// The file in local host should be deleted.
Please give it a shot.
Related
I read a lot of threads here about move_uploaded_file() here, but I cannot find the answer. Im trying code from this website - https://www.simplilearn.com/tutorials/php-tutorial/image-upload-in-php but it doesnt work.
I have 3 files:
dbConfig.php
<?php
// Database configuration
$dbHost = "localhost";
$dbUsername = "****";
$dbPassword = "****";
$dbName = "image_test";
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
main.php
<!DOCTYPE HTML>
<html>
<head>
<title>#tyvlasystudio</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select Image File to Upload:
<input type="file" name="file">
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>
upload.php
<?php
// Include the database configuration file
include 'dbConfig.php';
$statusMsg = '';
echo $_FILES;
// File upload path
$targetDir = "uploads/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
echo $targetDir ."<br>";
echo $fileName ."<br>";
echo $targetFilePath ."<br>";
echo $fileType ."<br>";
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif','pdf');
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
// Insert image file name into database
$insert = $db->query("INSERT into images (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");
if($insert){
$statusMsg = "The file ".$fileName. " has been uploaded successfully.";
}else{
$statusMsg = "File upload failed, please try again.";
}
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
// Display status message
echo $statusMsg;
?>
But it doesnt worked.
Here is result:
I dont know what is bad in this code. Thanks for your advice
Edit 16.2.2023 - 23:00
I figured it out, that problem maybe will be in POST metod. Ive tried now easy code with two files:
main1.php
<html>
<body>
<form method="post" action="main2.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
</body>
</html>
main2.php
<?php
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
?>
But only with GET method I'll get right answer.
this works for me. please add required attribute to input
<input type="file" name="file" required>
and replace echo $_FILES; //get notice error to echo '<pre>'.print_r($_FILES, true).'</pre>';
result:
Array
(
[file] => Array
(
[name] => 314370969_1174754369788841_1709138246209437707_n.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp_new\tmp\php6BBB.tmp
[error] => 0
[size] => 275646
)
)
uploads/
314370969_1174754369788841_1709138246209437707_n.jpg
uploads/314370969_1174754369788841_1709138246209437707_n.jpg
jpg
The file 314370969_1174754369788841_1709138246209437707_n.jpg has been uploaded successfully.
and pleease check the post_max_size and upload_max_filesize value in php.ini because the defult value is 2MB
If you're having trouble uploading photos via PHP's move_uploaded_file function, there are a few potential causes to check for:
Permissions: Check that the directory where you're trying to move the uploaded file to has the correct permissions. It should be writable by the user that PHP is running as (usually the web server user).
File size limits: PHP has several settings that can limit the size of uploaded files. Check the upload_max_filesize and post_max_size settings in your php.ini file and adjust them as needed.
Form enctype: Make sure that the form that's being used to upload the file has the enctype attribute set to "multipart/form-data". Without this, the file data won't be properly encoded and won't be able to be uploaded.
File name: Make sure that the file name doesn't contain any invalid characters (such as slashes) that could interfere with the file path when trying to move it. You can use PHP's basename function to extract just the filename part of the path.
Destination path: Check that the destination path you're trying to move the file to is correct and exists. You can use PHP's is_dir function to check if the directory exists and mkdir to create it if it doesn't.
Error messages: Check for any error messages that may be generated during the upload process. You can use PHP's $_FILES['file']['error'] variable to check for errors. Possible error codes include UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, and UPLOAD_ERR_EXTENSION.
By checking these potential causes and making any necessary adjustments, you should be able to resolve the issue and successfully upload your photos via PHP's move_uploaded_file function.
Good day, I have been following a tutorial and have worked really hard to make this script work, I have managed to fix many things however the one constant problem I'm getting is:
No such file or directory in /home/xxxxxxx/public_html/regForm.php on line 93
What I am trying to acomplish is get user to upload a profile pic to my live server / website upon registration, here is part of my form and the code below:
I would greatly appreciate it if a more experienced user can give it a scan and advise to where I am going wrong, thanx in advance
HTML FORM
<form ENCTYPE="multipart/form-data" name="registration-form" id="regForm" method="post" action="regForm.php">
:
:
Upload Picture: <INPUT NAME="file_up" TYPE="file">
<input type="submit" value="Register" name="register" class="buttono" />
</form>
UPLOAD SCRIPT
$file_upload="true";
$file_up_size=$_FILES['file_up']['size'];
echo $_FILES['file_up']['name'];
if ($_FILES['file_up']['size']>250000){
$msg=$msg."Your uploaded file size is more than 250KB
so please reduce the file size and then upload.<BR>";
$file_upload="false";
}
if (!($_FILES['file_up']['type'] =="image/jpeg" OR $_FILES['file_up']['type'] =="image/gif"))
{
$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";
}
$file_name=$_FILES['file_up']['name'];
if($file_upload=="true"){
$ftp_server = "xxxxxx";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username='xxxxxx';
$ftp_userpass='xxxxxx';
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = $file_name;
// upload file
if (ftp_put($ftp_conn, "public_html/img/userPics", $file, FTP_ASCII)) //here is the problem with the path I suspect
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
}
When dealing with images, you need to use a binary format.
FTP_BINARY instead of FTP_ASCII.
As per the manual:
http://php.net/manual/en/function.ftp-put.php
Make sure the path is correct. This can differ from different servers.
You also need a trailing slash here:
public_html/img/userPics/
^
Otherwise, your system will interpret that as:
userPicsIMAGE.JPG rather than the intended userPics/IMAGE.JPG
Yet, you may need to play around with the path itself. FTP is a bit tricky that way.
I.e.:
/home/xxxxxxx/public_html/userPics/
img/userPics/
../img/userPics/
Depending on the file's area of execution.
Root > ftp_file.php
-img
-userPics
The folder also needs to have proper permissions to be written to.
Usually 755.
Use error reporting if your system isn't already setup to catch and display error/notices:
http://php.net/manual/en/function.error-reporting.php
An example from the FTP manual:
<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );
?>
Another thing though, the use of of "true" and "false". You're most likely wanting to check for truthness
$file_upload=true;
rather than a string literal $file_upload="true";
same thing for
if($file_upload=="true")
to
if($file_upload==true)
and for $file_upload="false"; to check for falseness
to $file_upload=false;
Read the manual on this:
http://php.net/manual/en/language.types.boolean.php
This is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE.
Test your FTP connection, pulled from https://stackoverflow.com/a/3729769/
try {
$con = ftp_connect($server);
if (false === $con) {
throw new Exception('Unable to connect');
}
$loggedIn = ftp_login($con, $username, $password);
if (true === $loggedIn) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
print_r(ftp_nlist($con, "."));
ftp_close($con);
} catch (Exception $e) {
echo "Failure: " . $e->getMessage();
}
Naming conventions:
I also need to note that on LINUX, userPics is not the same as userpics, should your folder name be in lowercase letters.
Unlike Windows, which is case-insensitive.
i simply want that after i upload an image using form file upload, it will display those images on the next page. The images are uploaded to a folder on the ftp. i have looked everywhere but cant seem to find the right solution.
this is the HTML code for uploading image (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 />
Choose a file to upload: <input name="uploadedfile_2" type="file" /><br />
<input type="submit" value="Upload Files" />
</form>
Here's the PHP code on the next page (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";
}
$file = $_FILES["uploadedfile_1"]["name"];
$file2 = $_FILES["uploadedfile_2"]["name"];
$remote_file_path = "/2013/img/".$file;
$remote_file_path2 = "/2013/img/".$file2;
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile_1"]["tmp_name"],FTP_ASCII);
ftp_put($conn_id, $remote_file_path2, $_FILES["uploadedfile_2"]["tmp_name"],FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>
Above is only the Upload code and I don't know what Php code I need to use for Displaying the images, but typically, I'd like to echo these two images on Upload.php. Is there a way that this code can predict what images were uploaded?
Any help would be much appreciated.
Here's a complete script you can try.
When the user uploads something on PHP, that something goes to a temporary directory with a temporary name. You should therefore move the file away from there to a desired location. That's what the move_uploaded_file() in the script does. Also, for the images to show, that path must be available to your web server, which would mean under the public_html directory. And for PHP to be able to move the files there once they're uploaded, the directory must be writable. I've added some code to create a writable directory called uploads in the directory of the current script.
<html>
<head></head>
<body>
<?php
// The HTML beginning tags must be there or the output won't render
// as a web page
// Local path for storing the files, under the directory of this file
$local_path = dirname(__FILE__) . '/uploads';
// Create the directory if it doesn't exist
if ( ! is_dir( $local_path ) ) {
mkdir( $local_path );
}
// Make the directory writable
if ( ! is_writable( $local_path ) ) {
chmod( $local_path, 0777 );
}
// Loop through the uploaded files
foreach ( $_FILES as $ul_file ) {
// Check for upload errors
if ( $ul_file['error'] )
die( "Your file upload failed with error code " . $ul_file['error'] );
// Set a new file name for the temporary file
$new_file_name = $local_path . '/' . $ul_file['name'];
// Move the temporary file away from the temporary directory
if ( ! move_uploaded_file( $ul_file['tmp_name'], $new_file_name ) )
die( "Failed moving the uploaded file" );
// Store the local file paths to an array
$local_file_paths[] = $new_file_name;
}
// Now do your FTP connection
$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 "<p>Connected as $ftp_username # $ftp_server</p>";
} else {
die( "Could not log in as $ftp_username\n" );
}
// Loop through the local filepaths array we created
foreach ( $local_file_paths as $local_file_path ) {
// The remote file path on the FTP server is your string +
// the base name of the local file
$remote_file_path = "2013/img/" . basename( $local_file_path );
// Put the file on the server
ftp_put( $conn_id, $remote_file_path, $local_file_path, FTP_BINARY );
echo "<p>Uploaded a file to $remote_file_path</p>";
}
// Close the connection
ftp_close( $conn_id );
echo "<p>Connection closed. Your images are here:</p>";
// Loop through the file paths again and output an HTML IMG element for each
foreach ( $local_file_paths as $local_file_path ) {
echo "<img src='$local_file_path' alt='Your uploaded file' />";
}
// Final HTML tags
?> </body></html>
After the HTTP POST upload from your user, the files go to a temporary directory under a temporary name. The actual file names on your server are contained in $_FILES['uploadedfile_1']['tmp_name'] and $_FILES['uploadedfile_2']['tmp_name']. (The ['name'] is the original file name on the user's computer.) You should then check for upload errors, move the files away from the temporary directory, and then you can put them via FTP. Finally, you can output a HTML IMG element.
// Check for errors
if ( $_FILES['uploadedfile_1']['error'] )
die( "Upload failed with error code " . $_FILES['uploadedfile_1']['error'] );
// Set a new path for the file. Use the original file name.
$new_path = '/a/writable/path/on/your/system/' . $_FILES['uploadedfile_1']['name'];
// Move the file away from the temporary directory
if ( ! move_uploaded_file( $_FILES['uploadedfile_1']['tmp_name'], $new_path ) ) {
die( "Failed moving the uploaded file" );
}
// Put the file on the FTP connection established before
// Use the FTP_BINARY type for image files
ftp_put($conn_id, $remote_file_path, $new_path,FTP_BINARY);
// After closing the connection, output an IMG element
// This works if your $new_path is readable by the web server
echo "<img src='$new_path' alt='Your uploaded file' />";
<html>
<head></head>
<body>
<?php
// The HTML beginning tags must be there or the output won't render
// as a web page
if ($_POST) {
// Local path for storing the files, under the directory of this file
$local_path = dirname(__FILE__) . '/uploads';
// Create the directory if it doesn't exist
if (!is_dir($local_path)) {
mkdir($local_path);
}
// Make the directory writable
if (!is_writable($local_path)) {
chmod($local_path, 0777);
}
// Loop through the uploaded files
foreach ($_FILES as $ul_file) {
// Check for upload errors
if ($ul_file['error']) {
die("Your file upload failed with error code " . $ul_file['error']);
}
// Set a new file name for the temporary file
$new_file_name = $local_path . '/' . $ul_file['name'];
// Move the temporary file away from the temporary directory
if (!move_uploaded_file($ul_file['tmp_name'], $new_file_name) ) {
die("Failed moving the uploaded file");
}
// Store the local file paths to an array
$local_file_paths[] = $new_file_name;
}
// Now do your FTP connection
$ftp_server = "server";
$ftp_username = "username";
$ftp_password = "password";
$conn_id = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
if (#ftp_login($conn_id, $ftp_username, $ftp_password)) {
echo "<p>Connected as $ftp_username # $ftp_server</p>";
} else {
die("Could not log in as $ftp_username\n");
}
// Loop through the local filepaths array we created
foreach ($local_file_paths as $local_file_path) {
// The remote file path on the FTP server is your string +
// the base name of the local file
$remote_file_path = basename( $local_file_path );
// Put the file on the server
ftp_put( $conn_id, $remote_file_path, $local_file_path, FTP_BINARY );
echo "<p>Uploaded a file to $remote_file_path</p>";
}
// Close the connection
ftp_close($conn_id);
}
?>
<form enctype="multipart/form-data" action="img_upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploadedfile_1" type="file" /><br />
Choose a file to upload: <input name="uploadedfile_2" type="file" /><br />
<input type="submit" value="Upload Files" />
</form>
</body>
</html>
I have a php file that uploads images like jpegs and png onto a folder called uploads that is stored on the apache server and in the same location as the php file.
I have checked the code of both the HTML and the PHP and both seem to be perfectly fine, however whenever I try to upload a file I always get an error message and the file doesn't get uploaded.
It would be much appreciated if someone with more experience than me can look at my code and tell me why it is behaving in this manner.
Here is the HTML form:
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload Your File</title>
</head>
<body>
<?php
// put your code here
?>
<form enctype="multipart/form-data" method="post" action="fileHandler.php">
Select File:
<input name="uploaded_file" type="file"/><br/>
<input type="submit" value="Upload"/>
</form>
</body>
</html>
and here is the PHP file that is executed when the form is submitted:
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* PHP file that uploads files and handles any errors that may occur
* when the file is being uploaded. Then places that file into the
* "uploads" directory. File cannot work is no "uploads" directory is created in the
* same directory as the function.
*/
$fileName = $_FILES["uploaded_file"]["name"];//the files name takes from the HTML form
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"];//file in the PHP tmp folder
$fileType = $_FILES["uploaded_file"]["type"];//the type of file
$fileSize = $_FILES["uploaded_file"]["size"];//file size in bytes
$fileErrorMsg = $FILES["uploaded_file"]["error"];//0 for false and 1 for true
$target_path = "uploads/" . basename( $_FILES["uploaded_file"]["name"]);
echo "file name: $fileName </br> temp file location: $fileTmpLoc<br/> file type: $fileType<br/> file size: $fileSize<br/> file upload target: $target_path<br/> file error msg: $fileErrorMsg<br/>";
//START PHP Image Upload Error Handling---------------------------------------------------------------------------------------------------
if(!$fileTmpLoc)//no file was chosen ie file = null
{
echo "ERROR: Please select a file before clicking submit button.";
exit();
}
else
if(!$fileSize > 16777215)//if file is > 16MB (Max size of MEDIUMBLOB)
{
echo "ERROR: Your file was larger than 16 Megabytes";
unlink($fileTmpLoc);//remove the uploaded file from the PHP folder
exit();
}
else
if(!preg_match("/\.(gif|jpg|jpeg|png)$/i", $fileName))//this codition allows only the type of files listed to be uploaded
{
echo "ERROR: Your image was not .gif, .jpg, .jpeg or .png";
unlink($fileTmpLoc);//remove the uploaded file from the PHP temp folder
exit();
}
else
if($fileErrorMsg == 1)//if file uploaded error key = 1 ie is true
{
echo "ERROR: An error occured while processing the file. Please try again.";
exit();
}
//END PHP Image Upload Error Handling---------------------------------------------------------------------------------------------------------------------
//Place it into your "uploads" folder using the move_uploaded_file() function
$moveResult = move_uploaded_file($fileTmpLoc, $target_path);
//Check to make sure the result is true before continuing
if($moveResult != true)
{
echo "ERROR: File not uploaded. Please Try again.";
unlink($fileTmpLoc);//remove the uploaded file from the PHP temp folder
}
else
{
//Display to the page so you see what is happening
echo "The file named <strong>$fileName</strong> uploaded successfully.<br/><br/>";
echo "It is <strong>$fileSize</strong> bytes.<br/><br/>";
echo "It is a <strong>$fileType</strong> type of file.<br/><br/>";
echo "The Error Message output for this upload is: $fileErrorMsg";
}
?>
make sure that the directory structure has write permissions. You can check within php by using is_writeable. By checking from within PHP you will also be making sure that the PHP user has write access.
Check the folder permissions on the server. If incorrect, you can modify your php.ini file.
Is there a way to upload a file to server using php and the filename in a parameter (instead using a submit form), something like this:
myserver/upload.php?file=c:\example.txt
Im using a local server, so i dont have problems with filesize limit or upload function, and i have a code to upload file using a form
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="fileForm" enctype="multipart/form-data">
File to upload:
<table>
<tr><td><input name="upfile" type="file"></td></tr>
<tr><td><input type="submit" name="submitBtn" value="Upload"></td></tr>
</table>
</form>
<?php
if (isset($_POST['submitBtn'])){
// Define the upload location
$target_path = "c:\\";
// Create the file name with path
$target_path = $target_path . basename( $_FILES['upfile']['name']);
// Try to move the file from the temporay directory to the defined.
if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['upfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
</body>
Thanks for the help
Image Upload Via url:
//check
$url = "http://theonlytutorials.com/wp-content/uploads/2014/05/blog-logo1.png";
$name = basename($url);
try {
$files = file_get_contents($url);
if ($files) {
$stored_name = time() . $name;
file_put_contents("assets/images/product/$stored_name", $files);
}
}catch (Exception $e){
}
If you're doing this localy do you have the need to use "upload" mechanisms? otherwise if you're sending something externaly you could use cURL to upload the file, and run the PHP script on the CLI, quick google: http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
if you want to use url to send the name of the file like below:
www.website.com/upload.php?hilton_plaza_party.jpg
There is a script to do that. Here: http://valums.com/ajax-upload/