views:

256

answers:

3

Hi,

I am currently working on a Joomla! website. I am using the Jumi extension (which allows custom scripts to be made and executed within Joomla! itself) to create a simple file upload tool. The problem is that I get the following error:

Warning: copy(C:/xampp/htdocs/images/1253889508.jpg) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\Joomla\components\com_jumi\jumi.php(25) : eval()'d code on line 61

The offending code is as follows:

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname='C:/xampp/htdocs/images/'.$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);

The full code is available here. Once I get this working, I will be modifying it to meet the needs of my website.

I'm not sure if the problem is a permissions issue with Jumi or if there is some other problem. The best I can tell is that for whatever reason, the temp file is not being created.

Thanks for any tips you may have!

A: 

Forward slashes in the $namename path rather than backslashes?

Gav
Hmm... that does look more correct, but I'm afraid it's still throwing the same error. Thanks for the ridiculously speedy response, though!
Greg Zwaagstra
Windows allows you to use forward slashes in path names.
R. Bemrose
Windows accepts both, so thats probably not the sollution
Gerrit
Didn't actually know that- sorry; was just comparing the paths in the example provided and vaguely remembering that Windows uses backslashes in the command line.
Gav
+3  A: 

does your C:/xampp/htdocs/images directory actually exists?
if not create it manually or with mkdir()

also try to use the constant DIRECTORY_SEPARATOR instead of hardcoding slashes

Also you should use the move_uploaded_file() for this and not the copy() function.

And never hardcode absolute paths into your scripts! instead get the root path and preferibly set it as a constant, this is mostly done with the dirname() function in the entry file, but joomla allready has a constant you can use for this.

Nicky De Maeyer
This is rather embarrasing... I forgot a directory between htdocs and images. Thanks, fellow! I'll be sure to use the DIRECTORY_SEPERATOR as well.
Greg Zwaagstra
copy() is the wrong way of doing this!
tharkun
he should use absolute paths, but not hardcoded....
Nicky De Maeyer
2 things: A you should not hardcode your paths because they will only work on your machine = bad practice, error source! B you want to use move because otherwise you'll have to write another line of code for deleting the file from the temp directory or you will let your temp directory fill up with trash.
tharkun
@NickyDeMaeyer: yes, you're right!
tharkun
+1 for the edit!
tharkun
Using the Joomla constant JPATH_BASE and it still works so thanks for that as well!
Greg Zwaagstra
that's the way JPATH_BASE!
tharkun
+6  A: 

Try this:

if(move_uploaded_file($_FILES['image']['tmp_name'], $newname)){
    // move worked, carry on
}

And use relative paths instead of absolute ones.

inkedmn
copy() is indeed the wrong method since the file has to be moved from the temporary directory. move_uploaded_file() does a copy from that tmp dir
Nicky De Maeyer
I'd say move_uploaded_file is the more obvious choice since it's clearly designed to solve exactly this problem, but it's your code :)
inkedmn
Even though the copy() is now working, is it still better to try the move_uploaded_file? Also, how do I use relative paths if the directory is in a very specific place?
Greg Zwaagstra
yes, because you don't want to copy the file, you want to move it.
tharkun
If you're going to be saving uploaded files into the same directory (or a sub of that directory), I'd define a constant somewhere and build your paths relative to that.
inkedmn
Cool! I think that clears everything up! Within 15 minutes, too! Crazy website.
Greg Zwaagstra