Jump to content

Archived

This topic is now archived and is closed to further replies.

MrMastermind

Upload file script works directly in script, but not as function

Recommended Posts

Hi,

I have this simple script that uploads a file. I thought I'd write it into a function, so I could call it more easily. But when I use the function I get a permission denied error, while the script works perfectly when it is directly in the other script. Can anyone tell me why?:

[code]
function fupload ($path, $file){
//Upload grafiek naar server
if (trim($file['name'])!=""){
$newfile = $file['name'];
$filename_tmp = $file['tmp_name'];
$grafiek = $file['name'];

if (is_uploaded_file($filename_tmp)){
$save_loc = $path . $newfile;

// Move the Uploaded file to the correct Directory
if(move_uploaded_file($filename_tmp, $save_loc)){
chmod("$save_loc",0777);
}
}
}else{
return $curfile;
}
}
[/code]

[code]//Upload grafiek naar server
$file_dir  = "../_files-cms/Image/";

if (trim($_FILES['grafiek']['name'])!=""){
$newfile = $_FILES['grafiek']['name'];
$filename_tmp = $_FILES['grafiek']['tmp_name'];
$grafiek = $_FILES['grafiek']['name'];

if (is_uploaded_file($filename_tmp)){
$save_loc = $file_dir . $newfile;

// Move the Uploaded file to the correct Directory
if(move_uploaded_file($filename_tmp, $save_loc)){
chmod("$save_loc",0777);
}
}
}[/code]

Share this post


Link to post
Share on other sites
Could you include the code where you call this function? Would like to see what you are passing in as $path and $file...

Share this post


Link to post
Share on other sites
Have you just translated the 'grafiek' to 'image' for the purposes of this post? If not, then that is a difference already. I would have expected...

[code]

<?php

fupload($path, $_FILES['grafiek']);

?>

[/code]

Even so, I seem to remember having problems with this kind of thing in the past too. I had to break down the troubleshooting into stages - check the variables, check the temp file gets created, etc.

Share this post


Link to post
Share on other sites
Yes, sorry about that, I translated the variable for this forum. I can see the temp file gets created and the variables also match up. So I still haven't got a clue why this is not working.

Share this post


Link to post
Share on other sites
So is it failing on the move part of the function then? Are you using 2 separate scripts i.e. working.php and notworking.php? Check that the notworking.php script has permission to do what you are trying to do. Might be to do with the owner of the save location? If you are using 2 scripts, try putting the function into the working one.

Share this post


Link to post
Share on other sites

×

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.