View Full Version : PHP Upload to random dir.
Neoeyal
07-26-2006, 10:11 PM
I'm making an upload script with PHP
I want the uploaded files to be uploaded to a random directory.
I made something like this:
// the important values
$randit = rand(100000, 999999);
$dir = "imgs/" . $randit . "/";
$uploadfile = $dir . $userfile_name;
if (mkdir("imgs/" . $randit) && chmod("imgs/" . $randit, 0777)) // make the dir
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
// file uploaded URL here
}
else
{
echo "Couldn't upload";
}
}
of course this isn't the whole script.. i wrote just the important parts of the script
after all of this i get "Couldn't upload", the dir was made with 0777 CHMOD but it didn't upload the file
what is my mistake ?
blm126
07-26-2006, 11:27 PM
$_FILES['userfile']['tmp_name'] should be $_FILES['uploadedfile']['tmp_name']
Neoeyal
07-27-2006, 08:24 PM
userfile is the name of the File <input>
i can't change it.
anyways, if i try to upload the file without making a random directory it works
i can't seem why the script doesn't work
$uploadfile = $dir . $userfile_name;should be
$uploadfile = $dir . $_FILES['userfile']['name'];No register_globals here, so you're trying to give the file a blank filename, which isn't allowed.
Neoeyal
07-27-2006, 11:02 PM
it's not the whole script...
i wrote new script but it doesnt work too...
here it is and this time it's the whole:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META content="text/html; charset=windows-1255" http-equiv=Content-Type>
<title>NEOPROJECTS</title>
</head>
<body>
<?php
include('config.php');
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
if (!is_writeable($bdir)){
die ("Error: The directory <b>($bdir)</b> is NOT writable");
}
$size = $canup/1024/1024;
$rand_dir = rand(100000, 999999);
$mkdir = $bdir."/".$rand_dir."/";
echo "[<b>".$size." MB</b>]<br>";
echo "<form enctype=\"multipart/form-data\" method=\"post\" action=\"?action=upload\">
<input type=\"file\" name=\"userfile\">
<br>
<input type=submit name=submit value=Send></form>";
if($_GET['action'] == 'upload')
{
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_name = str_replace(" ", "", $file_name);
$file_name = strtolower($file_name);
$file_size = $_FILES['userfile']['size'];
$file_tmp = $_FILES['userfile']['tmp_name'];
$ext = strrchr($file_name,'.');
$ext = str_replace(" ", "", $ext);
$ext = strtolower($ext);
if(!is_uploaded_file($file_tmp))
{
echo "Error: Please select a file to upload!";
exit();
}
if (($extlimit == "yes") && (!in_array($ext,$limitedext)))
{
echo "Error: Wrong file extension";
exit();
}
$rand_name = rand(10, 99);
if($file_size == 0)
{
echo "Error: File size is 0";
exit();
}
if($file_size == $canup || $file_size > $canup)
{
echo "Error: File size is bigger than <b>".$size." MB</b>";
exit();
}
mkdir($mkdir) or die ("Directory could not be created.");
chmod($mkdir, 0777) or die ("Directory could not be CHMOD.");
$mkdir = $mkdir.$rand_name.$file_name;
if (move_uploaded_file($file_tmp, $mkdir))
{
echo "Direct link <input type=text size=45 onFocus=\"this.select()\" value=".$url.$mkdir.">";
echo "<br>Forums link <input type=text size=45 onFocus=\"this.select()\" value=".$url.$mkdir.">";
} else {
echo "Error: A problem occurred while trying to upload the file";
exit();
}
}
?>
<br>
<a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-html401"
alt="Valid HTML 4.01 Transitional" height="31" width="88" border="0"></a>
</body>
</html>
this is the config.php :
<?
$url='http://eyal.webkit.biz/MsU/';
$bdir='imgs';
$canup='5242880';
$extlimit='yes';
?>
BTW
The script works good if i give it a specific directory and not a random.
the idea is to create a random directory and upload the file to it
Where does that one fail, then?
Neoeyal
07-27-2006, 11:19 PM
mkdir($mkdir) or die ("Directory could not be created.");
chmod($mkdir, 0777) or die ("Directory could not be CHMOD.");
$mkdir = $mkdir.$rand_name.$file_name;
if (move_uploaded_file($file_tmp, $mkdir))
{
echo "Direct link <input type=text size=45 onFocus=\"this.select()\" value=".$url.$mkdir.">";
echo "<br>Forums link <input type=text size=45 onFocus=\"this.select()\" value=".$url.$mkdir.">";
} else {
echo "Error: A problem occurred while trying to upload the file";
exit();
}
this part..
I get "Error: A problem occurred while trying to upload the file"
Neoeyal
07-29-2006, 10:51 PM
any help please?
Add error_reporting(E_ALL); to the top of that code.
Neoeyal
07-30-2006, 10:28 AM
ok.. but i get nothing
maybe there is another way to make a random directory while submitting the form and then to upload the file to this directory?
If it's returning FALSE but no warning is generated, it's because the file is not a valid uploaded file.
Neoeyal
07-30-2006, 03:05 PM
i don't know how to use this function for getting TRUE or FALSE
what should I do?
You already have, and I've absolutely no idea what you should do :p You could, however, use rename() instead of move_uploaded_file(), with the same arguments.
Neoeyal
07-30-2006, 10:46 PM
ok thanks anyway dude
it isn't so important =]
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.