Lets create a Free Image Hosting Script. This code is a PHP script that allows users to upload images or files to a server and provides a unique download link for each file. The script uses PHP’s file upload capabilities to process and validate the uploaded file, and then generates a unique URL for the file to be downloaded. Additionally, it also displays a list of all previously uploaded files on the server.
//Create a file called index.php
<?php
// Check if the form has been submitted
if (isset($_FILES['image'])) {
// Process the uploaded image
$image = $_FILES['image'];
$image_name = $image['name'];
$image_tmp = $image['tmp_name'];
$image_size = $image['size'];
$image_error = $image['error'];
$image_ext = explode('.', $image_name);
$image_ext = strtolower(end($image_ext));
$allowed = array('jpg', 'jpeg', 'png', 'gif', 'zip', 'rar');
if(in_array($image_ext, $allowed)){
if($image_error === 0){
if($image_size <= 5000000){
$image_name_new = uniqid('', true) . '.' . $image_ext;
$image_destination = 'uploads/' . $image_name_new;
if(move_uploaded_file($image_tmp, $image_destination)){
// Generate a unique URL for the image
echo "<div class=\"sMa3\"></div><script async=\"async\" src=\"//sharemyads.com/view/728/?uid=YOUR-SHARE-MY-ADS-ID\"></script><br/>";
$url = "https://CHANGETHISTOYOURDOMAIN.COM/uploads/" . $image_name_new;
echo 'Image uploaded successfully. <br>';
echo '<a href="' . $url . '" target="_blank">' . $url . '</a> <br>';
echo '<img src="' . $url . '" width="200" height="200">';
echo '<br> <textarea id="image_url" style="width:100%;">'. $url .'</textarea>';
echo '<br> <button onClick="copyUrl()">Copy URL</button>
<script>
function copyUrl() {
var copyText = document.getElementById("image_url");
copyText.select();
document.execCommand("copy");
alert("Copied the URL: " + copyText.value);
}
</script>';
}
}
}
}else{
echo "Invalid file format";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Image/File</title>
<style>
</style>
</head>
<body>
<h1>File/Image Upload</h1>
<p>upload images or files and get a special download link max of 5mb per upload. Your dl link will display once file is uploaded and will only display once so save it.</p>
<!--Place your add code below or join sharemyads.com-->
<div class="sMa3"></div><script async="async" src="//sharemyads.com/view/728/?uid=YOUR-SHARE-MY-ADS-ID"></script><br/>
<!--Place your add code above or join sharemyads.com-->
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Upload">
</form>
<h1>Uploaded Files</h1>
<ul>
<?php
$dir = "uploads/";
$files = scandir($dir);
foreach ($files as $file) {
if ($file == "." || $file == "..") continue;
echo "<li><a href='$dir/$file'>$file</a></li>";
}
?>
</ul>
</div>
</body>
</html>
//Create a folder called uploads
To use this code, you will need to create a PHP file and save it on a server that supports PHP. You will also need to create a directory named “uploads” in the same directory as the PHP file to store the uploaded files. The directory must have write permissions so that PHP can save the uploaded files to the directory.
The code first checks if the form has been submitted using the isset()
function. If the form has been submitted, it then processes the uploaded file using the $_FILES
superglobal array. The script extracts the necessary information about the uploaded file such as the file name, temporary file location, file size, error, and file extension.
Next, the script checks if the file extension is allowed by comparing it with an array of allowed file extensions. If the extension is allowed, the script checks if the file size is within the limit of 5MB (5000000 bytes). If the file passes both checks, a unique name is generated for the uploaded file using the uniqid()
function and the file is saved to the “uploads” directory using the move_uploaded_file()
function.
After the file has been uploaded successfully, the script generates a unique URL for the file, displays the URL to the user, and provides a button to copy the URL to the clipboard using a JavaScript function. The script also displays a list of all previously uploaded files on the server, which is obtained using the scandir()
function.
Finally, the HTML code is displayed, which contains a simple form for uploading files and displaying the list of previously uploaded files. The HTML code also includes a section for displaying ads, which can be customized or replaced with a different ad code.
Overall, this code provides a simple and straightforward way to upload files to a server and generate a unique download link for each file. However, it is important to note that this code does not provide any security measures to prevent unauthorized file uploads or protect against malicious files. Therefore, it is recommended to implement additional security measures to prevent any security vulnerabilities.