user5084957
user5084957

Reputation:

Changing the URL on file upload

Currently I have created a file upload script based off of the documentation found on this page.

I am currently trying to make it so that when a user uploads a file instead of going to "upload.php" it will go to it's own random random id url.

my current upload.php file consist of the following content.

<?php
$target_dir = "uploads/";
$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"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
?> 
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title>File: <?php echo basename( $_FILES["fileToUpload"]["name"]); ?></title>
<link href="style2.css" rel="stylesheet">
<style>
img {
max-width: 100%;
max-height: 100%;
}
</style>
</head>
<body>

<br />
<div class="container main">
<div class="row">
<div class="col-md-8">

<?php

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "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)) {
    echo "  
    <center><img src='/uploads/". basename( $_FILES["fileToUpload"]["name"]). "' alt='Some File' height='' width=''></center>
    ";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}

?>
<hr>
</div>
</div>
</div>
</body>
</html>

So end result would be to have a script which when the file is uploaded, it creates a url similar to example.com/random-id as the redirected process page.

Upvotes: 0

Views: 449

Answers (2)

Exhibitioner
Exhibitioner

Reputation: 123

After the file upload has succeeded you can redirect the user to another page via header();

Example:

$uploadComplete = true; // this is just an example variable, the real code which I have put below would be placed after your upload is actually complete.

header('Location: http://yoursite.com/'.$randomID); 

Or you can shorten it if you just want it to be going to the same root /$randomID like so.

header('Location: /'.$randomID);

exit(); // use this to prevent any scripts after the header has been sent from executing.

Upvotes: 2

desbest
desbest

Reputation: 4906

Use the header function for a redirect.

header("Location: http://example.com/$randomid/");

As the header function produces a HTTP header, make sure that there is no HTML or echo markup before a header.

A header function is preferred to a meta refresh, as header does a redirect without loading any content so it's faster.

Upvotes: 1

Related Questions