7 Replies - 289 Views - Last Post: 09 March 2014 - 07:00 AM Rate Topic: -----

#1 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 850
  • Joined: 06-July 13

While (file_exists('/home/.../'))

Posted 09 March 2014 - 04:27 AM

I've had to change my code for my upload because of the MySQL Server error. So instead, I've made the data insert before the upload and it will work. However, if it is a large file uploading, then it will take a while to upload, and in the sidebar of every page, if the user is an admin, it will show the amount of unchecked files.

So if for some unfortunate reason, the admin finds a file that doesn't have a file on the server yet (because it's still uploading) then they might delete it thinking it's not properly working. To resolve this problem, I attempted to create something to detect if the file existed on the server, and if it is it will count it as not uploaded yet. But, when a file is uploading, then it doesn't work and says that the maximum script execution time of 30 seconds has passed.

I am very new to while loops, and I know it only does something while a certain condition is true (obviously) but can this below theoretically work?

if ($pun_user['downloads_moderator'] == '1' || $pun_user['g_global_moderator'] == '1' || $pun_user['g_id'] == PUN_ADMIN)
{
	$uploading_files = '0';
	$uploaded_files = '0';
	$admmod = $shn_sites->prepare("SELECT COUNT(*) AS id FROM downloads_unchecked");
	$admmod->execute();
	$count = $admmod->fetch();

	$admmod = $shn_sites->prepare("SELECT file FROM downloads_unchecked");
	$admmod->execute();
	foreach ($admmod as $row)
	{
		while (!file_exists('/home/download_directory/'.$row['file'].''))
		{
			$uploading_files = $uploading_files + 1;
		}
		
		while (file_exists('/home/download_directory/'.$row['file'].''))
		{
			$uploaded_files = $uploaded_files + 1;
		}
		
	}
//I then later echo the values in the html and end the script with a "}" for the first if ($pun_user...



Is This A Good Question/Topic? 0
  • +

Replies To: While (file_exists('/home/.../'))

#2 pedro93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-November 12

Re: While (file_exists('/home/.../'))

Posted 09 March 2014 - 05:50 AM

For starters you can change
$uploading_files = $uploading_files + 1;


and
$uploaded_files = $uploaded_files + 1;


to this:
$uploading_files++;


and
$uploaded_files++;



That will increase the variable value by +1 :)

Can you show the code where you echo the file names?

Peter
Was This Post Helpful? 0
  • +
  • -

#3 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 850
  • Joined: 06-July 13

Re: While (file_exists('/home/.../'))

Posted 09 March 2014 - 05:56 AM

Following on directly from the end of the code above:

	?>		
<div class="boxed">
<h2 class="heading">Admin</h2>
			<div class="content">
				<ul>
				<?php 
		if ($uploaded_files > 0)
		{
?>
<li class="first"><a href='http://admin.downloads.mysite.com/'><?php echo $uploaded_files; ?> New Files</a></li>
<?php 
		}
		else
		{
?>
<li class="first"><a href='http://admin.downloads.mysite.com/'>No New Files</a></li>
<?php
		}
 

	$admmod2 = $shn_sites->prepare("SELECT COUNT(*) AS id FROM downloads WHERE approval = '1'");
	$admmod2->execute();
	foreach ($admmod2 as $row)
	{	
	
		if ($row['id'] > 0)
		{
?>					
<li><a href="http://admin.downloads.mysite.com/updated_files.php"><?php echo $row['id']; ?> Updated Files</a></li>
<?php 
		}
		else
		{
?>
<li><a href="http://admin.downloads.mysite.com/updated_files.php">No Updated Files</a></li>
<?php
		}

	}
	
		if ($uploading_files > 0)
		{
?>					
<li><?php echo $uploading_files; ?> Files Currently Uploading</li>
<?php 
		}
		else
		{
?>
<li>No Files Currently Uploading</li>
<?php
		}

?>

				</ul>
			</div>
		</div>


This post has been edited by chris98: 09 March 2014 - 05:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 pedro93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-November 12

Re: While (file_exists('/home/.../'))

Posted 09 March 2014 - 06:07 AM

In this loop on line 23 of your post:
foreach ($admmod2 as $row)


You are calling all files stored in downloads. Is it possible that the files that are uploading are already on the database?

So file1.zip is added to the database, and it is still uploading, but $admmod2 already contains the file which is still uploading?

Peter
Was This Post Helpful? 0
  • +
  • -

#5 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 850
  • Joined: 06-July 13

Re: While (file_exists('/home/.../'))

Posted 09 March 2014 - 06:14 AM

They are files that have been updated and need re-checking, but yes they are in the database already - however, in the downloads_unchecked table rather than in the downloads table
Was This Post Helpful? 0
  • +
  • -

#6 pedro93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-November 12

Re: While (file_exists('/home/.../'))

Posted 09 March 2014 - 06:39 AM

View Postchris98, on 09 March 2014 - 01:14 PM, said:

They are files that have been updated and need re-checking, but yes they are in the database already - however, in the downloads_unchecked table rather than in the downloads table


Can you add
print_r($admmod2);

before:
foreach ($admmod2 as $row)


and check that the uploading files are not in the array?

Peter
Was This Post Helpful? 0
  • +
  • -

#7 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 850
  • Joined: 06-July 13

Re: While (file_exists('/home/.../'))

Posted 09 March 2014 - 06:51 AM

I've just got it - I changed the while() to if() and it has worked. I don't understand how, surely they do pretty much the same thing?
Was This Post Helpful? 0
  • +
  • -

#8 pedro93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 01-November 12

Re: While (file_exists('/home/.../'))

Posted 09 March 2014 - 07:00 AM

Of course! You are running a for loop, so by having a while loop there, you are checking if a file exists multiple times.

Each time in that for loop you just need an if statement because the if statement will check if the statement is true or not. Well spotted :)

Peter
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1