Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
make sure file is written to and closed before renaming
Old 08-11-2009, 05:44 AM make sure file is written to and closed before renaming
Novice Talker

Posts: 9
Trades: 0
Hi
This is all on XP. I am using CLI php to loop through a folder converting all .ppts to pdfs.
I use pdfcreator.exe for this. the relevant php line is:

$convertproc = proc_open('"C:\Program Files\PDFCreator\PDFCreator.exe" /NoStart /PF'.$file, $descriptorspec, $retval);

(I've also used exec() in earlier efforts).
I then get the process id using

proc_get_status($convertproc);

and pause until the process is finished, then close the process:

while($procstatus['running']==1 )
{
$procstatus = proc_get_status($convertproc);
}
proc_close($convertproc);

Then I try to rename the file:

$output = exec('ren '.$newfile .' '. $finalname, $output, $returnval);

However, the new pdf file seems to get written to *after* the process has finished running, so the rename fails. I've tried filesize(), is_readable(file) and all sorts of others but to no avail.

Any advice?
geekpie is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-11-2009, 10:32 AM Re: make sure file is written to and closed before renaming
Average Talker

Posts: 16
Trades: 0
You can of course just stick ';ren '.$newfile .' '. $finalname, $output, $returnval' onto the end of you PDFCreator command. And use exec().
Nill is offline
Reply With Quote
View Public Profile
 
Old 08-11-2009, 11:14 AM Re: make sure file is written to and closed before renaming
Novice Talker

Posts: 9
Trades: 0
I don't think so. I had it as a shell script before and I hit the same problem: the ren command was being executed before the file had been created by PDFCreator, or when PDFCreator was still being written to, so it was failing.
geekpie is offline
Reply With Quote
View Public Profile
 
Old 08-11-2009, 12:31 PM Re: make sure file is written to and closed before renaming
Average Talker

Posts: 16
Trades: 0
Quote:
Originally Posted by geekpie View Post
I don't think so. I had it as a shell script before and I hit the same problem: the ren command was being executed before the file had been created by PDFCreator, or when PDFCreator was still being written to, so it was failing.
Damm. Well it certainly would have worked bash.
Can't you just rename if before.
Maybe two loops:
1. rename all files
2. then pdfcreate-whatever all files.
or the other way round if pdfcreate requires the filename in a particular way.

wait did you set $procstatus before the while loop? ie:
Code:
$procstatus = proc_get_status($convertproc);
while($procstatus['running']==1 )
{
   $procstatus = proc_get_status($convertproc);
}
or just:
Code:
do {
    $procstatus = proc_get_status($convertproc);
 } while($procstatus['running']==1 )
Nill is offline
Reply With Quote
View Public Profile
 
Old 08-12-2009, 12:30 PM Re: make sure file is written to and closed before renaming
Novice Talker

Posts: 9
Trades: 0
Thanks for all the help.

I think I've solved it.

I couldn't rename the files before: the combination of the files being converted being ppt 2003, me having Office 2007 and PDFCreator seemed to insist on putting [Compatibility Mode] in the filename whatever I tried. I had PDFCreator on autosave with <title> as the output filename, but still always, [Compatibility Mode].

I did it using 2 delay loops: first one insisted new file existed before moving on

while (!file_exists($temp_filename)){
...

So all filenames at least existed by the time it got to the renaming part of the program. However the new file could still be being written to by PdfCreator, so a 2nd loop:

while ($returnval != 0)
{
exec('ren '.$file .' '. $finalname, $output, $returnval);
}
made sure the file got renamed.

Thanksagain.
geekpie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to make sure file is written to and closed before renaming
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33060 seconds with 12 queries