Download a file

Hi,

I’m running php to sql database. I have a file upload working and it uploads to a folder on a server. Now I need to download that file as I’ll be logged in as a different user.

The upload code is quite easy as it’s just a choose file button and then select the file.


<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
  
  
else
  {
  echo "Invalid file";
  }
  
  header("Location: student_content.php"); // redirect back
?>

Here’s the upload code.

Any info is greatly appreciated. Also, is there an easier way to code the upload so that it uploads any file type?? Instead of putting a load of file types in…

Cheers…

<a href="/path/to/file" download="FileName" title="FileName">
    <img src="/path/to/file" alt="FileName">
</a>

As seen here: http://stackoverflow.com/questions/2408146/href-image-link-download-on-click

As for upload any file, it’s a huge security risk so I wouldn’t recommend allowing any old file to be uploaded, so just add the types that you need to the $_FILES array and the in_array($extension, $allowedExts)) function will validate it.

Cheers for that man. I just need multiple file types like docs and images and was wondering if there was a shortcut for it instead of listing all the different types…

If I implement that code will it open a box and let me choose which files I need ? Lets say there’s 100 files named by number and I need 3 of them…

I’d keep it restricted to types, just edit that array and add your mime types/extension names. Here’s a full list if it helps: http://www.webmaster-toolkit.com/mime-types.shtml

I think you want:

  • application/pdf = .pdf
  • application/msword = .doc, .docx

You’ll need to edit your code to be something like:

[PHP]

<?php $allowedExts = array("gif", "jpeg", "jpg", "png", ".doc", ".docx", ".pdf"); $temp = explode(".", $_FILES["file"]["name"]); $extension = end($temp); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword")) && ($_FILES["file"]["size"] < 2000000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB
"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } header("Location: student_content.php"); // redirect back ?>

[/PHP]

Cheers thanks.

Have you any suggestions for the download problem?

I mentioned earlier about adding a download link:

[PHP]

Download your file

[/PHP]

That’s no use to me. I need a user to either open a box and select files or enter a file name from a form to download.

Also if I enter text into a form can I download it as a draft in a pdf?

Open a box? Not sure what you mean here. As for entering txt into a PDF using a form, I would use GhostScript to do this.

What I mean is when a person want’s to download a file it they click on a button that will open a window so the files can be selected and downloaded as the user doesn’t know the file names.

Just put a submit button in a form or use a hyperlink with $_GET variables.

[PHP]
Download Now
[/PHP]

And on the yourpage.php page you pull the files using the downloadID

[PHP]
$statement = $dbcon->prepare("
SELECT id, fileID, img
FROM downloads
WHERE id = :id
");
$statement->execute([
“:id” => $_GET[‘downloadID’]
]);
[/PHP]

And whatever fancy things you want to do with it. Give it a go, if you run into trouble, post your code and we’ll help you out.

I just have a basic html page that needs to be able to download multiple files from a location on a server.
I have posted my code for the upload. I need it to act the same for my download only it takes files from the server…

Can that code do that? I’m getting undefined variable $dbcon errors

Sponsor our Newsletter | Privacy Policy | Terms of Service