Jump to content

File Upload Functions: What Am I Doing Wrong?


filipposan87

Recommended Posts

here are 2 functions :

this is called upon like upfile('foto1',$_SESSION['userid'])

function upfile($file,$path){

$allowedExts = array("jpg", "jpeg", "gif", "png");

$extension = end(explode(".", $_FILES[$file]["name"]));

if ((($_FILES[$file]["type"] == "image/gif")

|| ($_FILES[$file]["type"] == "image/jpeg")

|| ($_FILES[$file]["type"] == "image/png")

|| ($_FILES[$file]["type"] == "image/pjpeg"))

&& ($_FILES[$file]["size"] < 2000000)

&& in_array($extension, $allowedExts))

{

if ($_FILES[$file]["error"] > 0)

{

echo "Return Code: " . $_FILES[$file]["error"] . "<br />";

}

else

{

echo "Upload: " . $_FILES[$file]["name"] . "<br />";

echo "Type: " . $_FILES[$file]["type"] . "<br />";

echo "Size: " . ($_FILES[$file]["size"] / 1024) . " Kb<br />";

echo "Temp file: " . $_FILES[$file]["tmp_name"] . "<br />";

 

if (file_exists("usrpics/".$path .'/'. $_FILES[$file]["name"]))

{

echo $_FILES[$file]["name"] . " already exists. ";

}

else

{

move_uploaded_file($_FILES[$file]["tmp_name"],

"usrpics/".$path.'/'. $_FILES[$file]["name"]);

echo "Stored in: " . "usrpics/" .$path. $_FILES[$file]["name"];

}

}

}

else

{

echo "Invalid file";

}

$_SESSION['path'] ='usrpics/'.$path.'/'.$_FILES["name"] ;

return TRUE ;

}

 

upload('foto1',$_SESSION['userid'])

function upload($file_id,$folder) {

$types="jpg,png,gif,jpeg";

$folder = 'usrpics/'.$folder ;

if(!$_FILES[$file_id]['name']) return array('','No file specified');

 

$file_title = $_FILES[$file_id]['name'];

//Get file extension

$ext_arr = split("\.",basename($file_title));

$ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension

 

//Not really uniqe - but for all practical reasons, it is

$uniqer = substr(md5(uniqid(rand(),1)),0,5);

$file_name = $uniqer . '_' . $file_title;//Get Unique Name

 

$all_types = explode(",",strtolower($types));

if($types) {

if(in_array($ext,$all_types));

else {

$result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any.

return array('',$result);

}

}

 

//Where the file must be uploaded to

if($folder) $folder .= '/';//Add a '/' at the end of the folder

$uploadfile = $folder . $file_name;

 

$result = '';

//Move the file from the stored location to the new location

if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) {

$result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any.

if(!file_exists($folder)) {

$result .= " : Folder don't exist.";

} elseif(!is_writable($folder)) {

$result .= " : Folder not writable.";

} elseif(!is_writable($uploadfile)) {

$result .= " : File not writable.";

}

$file_name = '';

 

} else {

if(!$_FILES[$file_id]['size']) { //Check if the file is made

@unlink($uploadfile);//Delete the Empty file

$file_name = '';

$result = "Empty file found - please use a valid file."; //Show the error message

} else {

chmod($uploadfile,0777);//Make it universally writable.

}

}

 

return array($file_name,$result);

}

 

the first one returnsvthe route /usrpics/ but lacks the filename and extension, but always prints out img error, regardless of the picture

 

the second one doesnt do anything.

 

it must be a function, it needs to process several photos wich fields are called

fotoX(1,2,3,and so on)

 

i just can't figure out what is wrong with these 2 functions.

 

i'd be really grateful if you could tell me what is wrong.

 

PS: im only using 1 function at a time.

Link to comment
Share on other sites

upfile('foto1',$_SESSION['userid'])
function upfile($file,$path){
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES[$file]["name"]));
if ((($_FILES[$file]["type"] == "image/gif")
|| ($_FILES[$file]["type"] == "image/jpeg")
|| ($_FILES[$file]["type"] == "image/png")
|| ($_FILES[$file]["type"] == "image/pjpeg"))
&& ($_FILES[$file]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[$file]["error"] > 0)
{
echo "Return Code: " . $_FILES[$file]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES[$file]["name"] . "<br />";
echo "Type: " . $_FILES[$file]["type"] . "<br />";
echo "Size: " . ($_FILES[$file]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES[$file]["tmp_name"] . "<br />";

if (file_exists("usrpics/".$path .'/'. $_FILES[$file]["name"]))
{
echo $_FILES[$file]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES[$file]["tmp_name"],
"usrpics/".$path.'/'. $_FILES[$file]["name"]);
echo "Stored in: " . "usrpics/" .$path. $_FILES[$file]["name"];
}
}
}
else
{
echo "Invalid file";
}
$_SESSION['path'] ='usrpics/'.$path.'/'.$_FILES["name"] ;
return TRUE ;
}

upload('foto1',$_SESSION['userid'])
function upload($file_id,$folder) {
$types="jpg,png,gif,jpeg";
$folder = 'usrpics/'.$folder ;
if(!$_FILES[$file_id]['name']) return array('','No file specified');

$file_title = $_FILES[$file_id]['name'];
//Get file extension
$ext_arr = split("\.",basename($file_title));
$ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension

//Not really uniqe - but for all practical reasons, it is
$uniqer = substr(md5(uniqid(rand(),1)),0,5);
$file_name = $uniqer . '_' . $file_title;//Get Unique Name

$all_types = explode(",",strtolower($types));
if($types) {
if(in_array($ext,$all_types));
else {
$result = "'".$_FILES[$file_id]['name']."' is not a valid file."; //Show error if any.
return array('',$result);
}
}

//Where the file must be uploaded to
if($folder) $folder .= '/';//Add a '/' at the end of the folder
$uploadfile = $folder . $file_name;

$result = '';
//Move the file from the stored location to the new location
if (!move_uploaded_file($_FILES[$file_id]['tmp_name'], $uploadfile)) {
$result = "Cannot upload the file '".$_FILES[$file_id]['name']."'"; //Show error if any.
if(!file_exists($folder)) {
$result .= " : Folder don't exist.";
} elseif(!is_writable($folder)) {
$result .= " : Folder not writable.";
} elseif(!is_writable($uploadfile)) {
$result .= " : File not writable.";
}
$file_name = '';

} else {
if(!$_FILES[$file_id]['size']) { //Check if the file is made
@unlink($uploadfile);//Delete the Empty file
$file_name = '';
$result = "Empty file found - please use a valid file."; //Show the error message
} else {
chmod($uploadfile,0777);//Make it universally writable.
}
}

return array($file_name,$result);
}

 

Just helping you get more replies. ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.