Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Image upload random name

Featured Replies

I am really struggling to firgure out how to create an image upload script that uploads an image an gives it a random name say the file was "image.jpg" - It would change it to something like "938765.jpg".

 

Can someone please edit my upload script so this happens and if possible explain what it is that makes it happen, thank you.

 

<?
$uploaddir = "companylogo";
$allowed_ext = "jpg, jpeg, png, png, bmp, gif"; 
$max_size = "10000";
$max_height = "100";
$max_width = "30"; 
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "File size is too big";
exit;
}
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
echo '<div align="left"><div id="indent">Your logo has been uploaded successfully, 
<a href="' .$uploaddir.'/'.$_FILES['file']['name'] .'">Click here to view.</a></div></div>';
} else {
print 'Please select a valid file.';
}
?>

When I want to create a completely random string I always use a current timestamp as an encryption key. This means that the string can never be repeated, in short:

 


$random = md5(time().uniqid());

 

Your script:

 


<?
$uploaddir = "companylogo";
$allowed_ext = "jpg, jpeg, png, png, bmp, gif"; 
$max_size = "10000";
$max_height = "100";
$max_width = "30"; 
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "File size is too big";
exit;
}
if(is_uploaded_file($_FILES['file']['tmp_name']))
{

/// we'd need to get the extension

$ext = pathinfo($_FILES['file']['name']);
$ext = $ext['extension'];

$random_filename = md5(time().uniqid());

move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$random_filename.".".$ext);
}
echo '<div align="left"><div id="indent">Your logo has been uploaded successfully, 
<a href="' .$uploaddir.'/'.$_FILES['file']['name'] .'">Click here to view.</a></div></div>';
} else {
print 'Please select a valid file.';
}
?>

Make sure you test, I wrote all that freehand ;)

  • Author

Yeeah I have, the 'click here to view' didn't have the randomised link in so I did this:

 

echo '<div align="left"><div id="indent">Your logo has been uploaded successfully, <a href="' .$uploaddir.'/' . $random_filename . '.' . $ext . '">Click here to view.</a></div></div>';

 

Works perfect!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.