Jump to content

Archived

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

Ninjakreborn

Files: if they tried to upload a file, work with file, if they didn't don't.

Recommended Posts

This isn't working, I want to test to see if they tried to upload a file, if they did, then I want to do some validation on the file, and then try to move it, check to make sure it exists, whatever.  BUt I only want to do that stuff if they tried to upload a file, because the file upload is optional, it's not required??  But when I test for the files variable before working on it, and don't upload a file, it still tries to work on the non-existent file, that would lead me to assume that the files array already has something stored in it, whether a file has been uploaded into memory or not, so how do I test to see if they attempted one.  because they can attempt an upload, and it not upload, so I still need to check for
if (!is_uploaded_file) {
$errorhandler .= "You file was not uploaded properly";
}
that is one of the things I need to test for, but I don't want to test for that, unless they attempted to upload one and it failed, not jsut if they didn't want to upload one in the first place
[code]<?php
if ($_FILES) {
    $newfile = "uploads/".basename($_FILES['file']['name']);
    $newfiletemp = "{$_FILES[file][tmp_name]}";
  if (file_exists($newfile)) {
    $errorhandler .= "The file already exists";
}
if (!move_uploaded_file($newfiletemp, $newfile)) {
$errorhandler .= "There was some sort of problem moving the file";
}
}
?>[/code]

Share this post


Link to post
Share on other sites
I use one of two methods, first off is to check if $_FILES['userfile']['name'] is empty, or check what $_FILES['userfile']['error'] is set to, if it is set to '4', the user did not select a file to upload.

Share this post


Link to post
Share on other sites
Ok I got it, but I am running into another problem.
Here is my code for reference
[code]<?php
// file handling
if ($_FILES['notefile']['name']) {
$newfile = "./uploads/notesandcheatsheets/".basename($_FILES['notefile']['name']);
$newfiletemp = "{$_FILES[notefile][tmp_name]}";
if (file_exists($newfile)) {
$errorha
ndler .= "The file already exists";
}
if (!move_uploaded_file($newfiletemp, $newfile)) {
$errorhandler .= "There was some sort of problem moving the file";

}
}else {
$newfile = "No file was uploaded";
$newfiletemp = "No file was uploaded";
}
// end file handling
?>[/code]
Ok there is the code, the problem I am having is it's not moving the file.  THe script that I am running for this is on a processor from the root folder it's from
/processors/post4.php
Now the file is being upload(from the root directory) to the following file
/uploads/notesandcheatsheets/
for some reason it's not moving it, I am saving the newfile and newfiletemp variables into the database, the newfile being the url, and hte newfiletemp being the name.
The thing is, it's not uploading, and I also need to make sure I can access the file later when calling the fileurl from the database.  So what do I need to do in this situation, and why isn't it working like this?
Also for the newfile variable, where I am setting the url, I tried a few things
I tried
/uploads/notesandcheatsheets
./uploads/notesandcheatsheets
but nothing seems to be allowing me to move the file.

Share this post


Link to post
Share on other sites
Is it coming back to you with an error?

Make sure your permissions on the directory you're moving the file too is correct as well as the file executing the move_uploaded_file().

Share this post


Link to post
Share on other sites
I checked permissions, that wasn't the problem.
I noticed a few issues, I had some of the folders named wrong so I fixed those.  ANd it is not 100% pointing to the right path, I am using this code below, and below that is the error it's giving me.
[code]<?php
// file handling
if ($_FILES['notefile']['name']) {
$newfile = "/upload/notesandcheatsheets/".basename($_FILES['notefile']['name']);
$newfiletemp = "{$_FILES[notefile][tmp_name]}";
if (file_exists($newfile)) {
$errorhandler .= "The file already exists";
}
if (!move_uploaded_file($newfiletemp, $newfile)) {
$errorhandler .= "There was some sort of problem moving the file";

}
}else {
$newfile = "No file was uploaded";
$newfiletemp = "No file was uploaded";
}
// end file handling
?>[/code]
Here is the error message below

[quote]Warning: move_uploaded_file(/upload/notesandcheatsheets/access information.txt): failed to open stream: No such file or directory in /home/content/b/c/h/bcharnas/html/processors/post4.php on line 272

Warning: move_uploaded_file(): Unable to move '/tmp/php4SyVSd' to '/upload/notesandcheatsheets/access information.txt' in /home/content/b/c/h/bcharnas/html/processors/post4.php on line 272
There was some sort of problem moving the file [/quote]

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.