Jump to content


Photo

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


  • Please log in to reply
5 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 September 2006 - 02:33 PM

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
<?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";
	}
}
?>

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 19 September 2006 - 02:37 PM

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.

#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 September 2006 - 03:15 PM

Ok I got it, but I am running into another problem.
Here is my code for reference
<?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
?>
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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 19 September 2006 - 03:26 PM

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().

#5 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 September 2006 - 03:42 PM

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.
<?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
?>
Here is the error message below

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


------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#6 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 September 2006 - 04:04 PM

got it, thanks

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users