Category Archives: Hosting

Can you recommend some post upload checks?

  1. Delete unwanted files, like ‘test’, ‘copy of’, etc.
  2. Give write permission (777) to the required folders.
  3. If .htaccess file has been uploaded, make sure it runs according the the server settings. Sometimes .htaccess files run on local but not on server.
  4. Make configuration settings according to server where required, like database connections, file paths, etc.
  5. Do not upload folders required for local copy of the web site like ‘backup’,'db’, etc – if you maintain them.
  6. In folders where there are no index/default files, place a blank index/default file to avoid directory listing. If this is not done, a site visitor can type in the folder path in browser URL and see all files in that folder which is not a good idea. This can also be done using .htaccess file with the following code. Options -Indexes

How to upload a file in PHP

The code to upload file in PHP is very simple, but we need to understand the flow which is a below.

  • Browse the file from a local system
  • Upload to server
  • Server keeps it on a temporary path
  • Copy from temporary to permanent path

Create a file upload form

<html>

<body>

<form action=”upload-file.php” method=”post” enctype=”multipart/form-data”>

<label for=”file”>Filename:</label>

<input type=”file” name=”file” id=”file” />

<br />

<input type=”submit” name=”submit” value=”Submit” />

</form>

</body>

</html>

Note:

  • An enctype attribute of the <form> tag has been specified. This attribute  specifies which content-type to use when submitting the form
  • We have used “multipart/form-data” to upload binary data, like the contents of a file, to be uploaded
  • If proper enctype is not provided, upload will not work.
  • File upload is a huge security risk so you must check what type of files are being uploade

Create a file upload script (upload-file.php)

<?php

$my_folder = “./uploads/”;

copy($_FILES["file"]["tmp_name"],$my_folder.$_FILES["file"]["name"]);

echo “File uploaded.”;

?>

This will upload the file to the specified path.

Note:

  • The default file upload size using a browser is usually 2MB so files larger than this size may not upload. You will have to alter the file upload setting on the server.
  • You need to set write permission to the folder where file needs to be upload. In our case, the “uploads” folder needs to have a 777 permission on a linux/unix server.

 

 

How to do server to server ftp transfer in PHP

It is quite difficult to transfer web site from one server to another. Here is a solution for server to server ftp transfer.

  • Zip the required files as one file. The zipping feature is usually available in your web hosting control panel’s file manager.
  • Upload the below mentioned script on the source server.
  • Run the script on the source server.
  • This will transfer files from one server to the other.
  • Unzip the files on the destination server.
This script can be used for other purposes as well.

<?php
$server = “ftp.you-server.com”; //address of ftp server (leave out ftp://)
$ftp_user_name = “userName”; // Username
$ftp_user_pass = “passwordHere”; // Password
$source = “/home/folder/public_html/filename.ext”;
$dest = “/public_html/filename.ext”;
$mode=”FTP_BINARY”;
$connection = ftp_connect($server);

$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);

if (!$connection || !$login) { die(‘Connection attempt failed!’); }

$upload = ftp_put($connection, $dest, $source, FTP_BINARY);

if (!$upload) { echo ‘FTP upload failed!’; }

ftp_close($connection);
echo “done”;
?>