Jump to content

[SOLVED] file_exists(), wtf?


gijew

Recommended Posts

So I've never come across this; ever.

 

file_exists($file);

 

Simple, right? Apparently not today. For whatever reason, it's returning false no matter what. Normally, I'd say user error (and it still may be, err, probably) but I can take the path it echo's back and pick up the file no problem. I can hard code it and it returns true. It's messing with my brain man!

 

I'd give the breakdown of how it's being used but I don't think it's relevant here. Syntax is pretty straight forward, it's file_exists() for Jebus' sake!

 

<?php
if (file_exists($this->path . $this->path_compile . $folder . $file)) {
  echo 'got it';
} else {
  echo 'tough luck, nope: ' . $this->path . $this->path_compile . $folder . $file;
}
?>

 

The path it returns on my dev box is: "C:/wamp/www/production/clcnationwide.com/compile/439992-1244098800-56700/439992.zip"

 

Now the folder name is auto-generated in 3 parts so here is where the error is most likely coming from.

 

Let's just analyze the auto-gen part so hopefully we can find a solution for my brain who isn't my friend today.

 

[part 1] - file number, it's like an internal auto-id that the document belongs to. Company thing. It stays the same.

[part 2] - time stamp but only using the date and not the time ( * mktime(0,0,0,date('m'),date('d'),date('y')) * )

[part 3] - random number between 1-99999

 

So, I'm thinking there are a number of areas this could go wrong here but I echo out the auto-gen number through each step and it comes back the same so I don't see where the problem is. Grr. For the sake of readability I'll shut up now.

Link to comment
Share on other sites

little fun

try this

<?php
if (file_exists($this->path . $this->path_compile . $folder . $file)) {
  echo 'got it';
} else {
  echo 'tough luck, nope: ' . $this->path . $this->path_compile . $folder . $file;
  if(is_dir($this->path))
  {
echo "Found: ".$this->path."<br />\n";
if(is_dir($this->path.$this->path_compile))
{
	echo "Found: ".$this->path.$this->path_compile."<br />\n";
	if(is_dir($this->path.$this->path_compile.$folder))
	{
		echo "Found: ".$this->path.$this->path_compile.$folder."<br />\n";
	}
}
  }
}
?>

Link to comment
Share on other sites

var_dump() comes back with the proper path *string(83) "C:/wamp/www/production/clcnationwide.com/compile/439992-1244098800-80365/439992.zip"*

 

And way to double, triple check there MadTechie but here are the results of your suggestion.

 

tough luck, nope: C:/wamp/www/production/clcnationwide.com/compile/439992-1244098800-52721/439992.zip

Found: C:/wamp/www/production/clcnationwide.com/

Found: C:/wamp/www/production/clcnationwide.com/compile/

Found: C:/wamp/www/production/clcnationwide.com/compile/439992-1244098800-52721/

 

You know, everything inside me says screw it, just write the code twice and stick it in the same method. I don't know what would be better at this point, fix it the crappy way or invest an entire day trying to fix it the right way. Lame! Such a simple function has never given me so much crap.

Link to comment
Share on other sites

updated

 

<?php
if (file_exists($this->path . $this->path_compile . $folder . $file)) {
echo 'got it';
}else{
echo 'tough luck, nope: ' . $this->path . $this->path_compile . $folder . $file;
if(is_dir($this->path))
{
	echo "Found: ".$this->path."<br />\n";
	if(is_dir($this->path.$this->path_compile))
	{
		echo "Found: ".$this->path.$this->path_compile."<br />\n";
		if(is_dir($this->path.$this->path_compile.$folder))
		{
			echo "Found: ".$this->path.$this->path_compile.$folder."<br />\n";
			$dir = $this->path.$this->path_compile.$folder;
			if ($dh = opendir($dir))
			{
				while (($file2 = readdir($dh)) !== false)
				{
					if($file2 == $file) echo "<B>FOUND:</B>"; //yeah right
					echo "filename: $file2 : <br />\n";
				}
				closedir($dh);
			}
		}
	}
}
}
?>

 

 

EDIT: cleaned tabs in code

Link to comment
Share on other sites

Nice thoughts on opening the directory to see if it's in there if it comes back as false. That gives me a whole new headache. PHP doesn't even see the file but I browse to the folder and it's there I'm tellin' ye!

 

This is making my noggin go in a new direction. There are two methods in use here.

 

Method 1: create_zip();

Method 2: download_zip();

 

Both methods are being used inside of a download_manager() method.

 

Method 2 is being called after the zip file is created (obviously). If I use the file_exists() function in the create_zip method after it's created, it comes back true. If I call it in the download_zip method after it IS created, it comes back false.

 

Unless you have any suggestions on how to debug that motha I'm going to play around with the code to see if I set something improperly. It's pretty straight forward but It's quite possible I screwed something up along the way. Thank you very much for the suggestions. It was very helpful!

Link to comment
Share on other sites

So I've concluded it has something to do with the way PHP is creating the zip file. I was using the PCLZip library but the documentation is horrible and PHP5's zip library is much cleaner imo. Anyhoo, here is what I'm coming up with for those that may be interested ;)

 

Folder contains:

439992 Table of Contents.txt

439992.zip

 

Both are generated by PHP. If I run file_exists() against the .txt file it comes back true. Even when I was looping through the directory using MadTechie's approach only the .txt file showed up BUT the .zip file WAS there. Has anyone come across this using PHP to create files? Kind of weird that it likes the .txt but not the .zip.

 

I'm going to play on Google but thought I'd throw it out there in case anyone has anything to throw in besides kill yourself now :)

Link to comment
Share on other sites

so it doesn't list the file!

 

Random tests

 

just some ideas

if (file_exists($this->path . $this->path_compile . $folder . $file)) {

to

$theFile = $this->path . $this->path_compile . $folder . $file;
if (file_exists($theFile)) {

 

Just to see,

 

If this is running on linux, check permissions

 

if your creating the file just before using file_exists() maybe add a sleep(2),

 

try renaming the file via the OS to "test" and then check it..

 

and if all else fails call the ghost busters

 

 

EDIT: just read your last post,

are you sure the zip file is being closed!

Link to comment
Share on other sites

Let me laugh and quote you at the same time. Don't worry, this is freaking hilarious!

 

EDIT: just read your last post,

are you sure the zip file is being closed!

 

Guess what the problem was? Thinking back to yesterday when I wrote this, my internal monologue was nagging at me to put $zip->close() after I finished. Right then, I looked at something else and said, I need to do that too! Guess what didn't make it back into my head?

 

HAHA! That's why they (we) say 99.9% of the time it's user error!

 

Thank you man, much appreciated. I will now put aside my Jimmy Jones brand Kool-Aid and put a shot of Patron silver in it's place to celebrate being DONE with this project!

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.