PHP - send file to user
Assuming that it's on the server:
readfile() — Outputs a file
NOTE: Just writing
readfile($file);
won't work. This will make the client wait for a response forever. You need to define headers so that it works the intended way. See this example from the official PHP manual:
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
How to send file from local computer to my server in php?
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)
Share files between users
In this case I may suggest that you add another field (column) to the user files table. This field may be called 'shared_users_id' and should hold a comma separated lists of ids of the shared users. For Ex.
Table: user_files
---------------------------------------
id | user_id | file | shared_users_id |
---------------------------------------
1 |1 |sample.jpg|2,3,4 |
---------------------------------------
Update 1:
When the share button is clicked, you can first make an AJAX request that list names of users available to be shared
<?php
// Select all from users
if(mysqli_num_rows($query) {
echo '<ul>';
while($users_lists = mysqli_fetch_assoc($query)) {
echo '<li data-id="'.$users_lists['id'].'">'.$users_lists['name'].'</li>';
}
echo '</ul>';
}
each list should carry the ids of the shares, so when the sharer clicks on any user, make another POST
request (maybe with AJAX) that carries the ID of the shared user, then retrieve this ID from the $_POST
handle block and execute the following action. For Ex we first assume that you have retrieved the lists of users when the sharer clicks the Share button.
// jQuery$("#ajaxUserLists li").click(function(e) {let user_id = $(this).data('id');$.post( "handle-share.php", {user_id: user_id});});
<ul id="ajaxUserLists"><li data-id="1">Vishal Limbachiya</li><li data-id="2">Erisan Olasheni</li><li data-id="3"></li></ul>
Upload a file using PHP
Below is one way to upload files, there are many other ways.
As @nordenheim said, $HTTP_POST_FILES
has been deprecated since PHP 4.1.0, thus not advisable to use so.
PHP Code (upload.php)
<?php
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if (isset($_POST["submit"])) {
if ($target_file == "upload/") {
$msg = "cannot be empty";
$uploadOk = 0;
} // Check if file already exists
else if (file_exists($target_file)) {
$msg = "Sorry, file already exists.";
$uploadOk = 0;
} // Check file size
else if ($_FILES["fileToUpload"]["size"] > 5000000) {
$msg = "Sorry, your file is too large.";
$uploadOk = 0;
} // Check if $uploadOk is set to 0 by an error
else if ($uploadOk == 0) {
$msg = "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$msg = "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
}
}
}
?>
HTML Code to start function
<form action="upload.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Hope this helps.
Best way to let users upload and share files from site?
You can use DropBox API for PHP. Let user upload the file on DropBox and return a link of uploaded file.
https://www.dropbox.com/developers-v1/core/start/php
Related Topics
Json_Decode Returns Null After Webservice Call
How to Generate All Permutations of a String in PHP
How to Put Composite Keys in Models in Laravel 5
Does PHP Have Short-Circuit Evaluation
Issue Reading Http Request Body from a Json Post in PHP
Replace the Variable Price Range by the Chosen Variation Price in Woocommerce 3
Global or Singleton For Database Connection
Remove All Attributes from HTML Tags
Create Array Tree from Array List
Select Count(*) from Table of MySQL in PHP
How to Upload Multiple Files Using PHP, Jquery and Ajax
Generate Json String from Multidimensional Array Data
Use Bound Parameter Multiple Times