Jump to content

Changeing file names

- - - - -

  • Please log in to reply
13 replies to this topic

#1
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
Dear programmers,

I am making a php script right now, and I don't know how to do something.

What does the script do?
gets a txt file, looks for information, checks it with a database.
when the file has been confirmed there has something to be done.

What has to be done?
The txt file that I scan for information has the same file name as a PDF file in a other map.
So if the txt file has been confirmed, the pdf file sould get a new name, and has to go to an other map.
The name that the pdf file should get is in the txt file.

so it goes like this:
- get a txt file
- look for $P_number.
- if $P_number has been foundm check it with the database

so far so good, from now i don't know what to do

- if it has been confirmed, look in an other map for a pdf file with the same name as the txt file.
- take the pdf file to a other map and give it a new name: $p_number
- delete txt file

I hope you guys or girls can help me!

Greetings,

William from The Netherlands

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
I'm not sure what do you mean by map, but my guess is a directory
So you'll need something like this

if (file_exists($dirOfPdf . $P_number . '.pdf')) { //Test if file exist

  rename($dirOfPdf . $P_number . '.pdf', $newDirOfPdf . $P_number . '.pdf'); //Move pdf

  unlink($dirOfTxt . $txt . '.txt'); //Delete the txt file

}

The function rename can be confusing a little bit, but we can use it to move file to an other directory

Edited by Vaielab, 09 September 2011 - 05:21 AM.
Adding some comments


#3
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
Thanks for the reply
so the code for me will be:

if ($cross_check => 1){

     if (file_exists("/var/www/html/william/pdf_source/" . $Document . '.pdf')) {

           rename("/var/www/html/william/pdf_source/" . $Document . '.pdf', "/var/www/html/william/pdf_result/" . $Po . '.pdf');

           }

	else:

          copy ("/var/www/html/william/pdf_source/" . $Document . '.pdf', "/var/www/html/william/pdf_result/" . $Document '.pdf');

		  unlink("/var/www/html/william/pdf_txt/" . $Document . '.txt');

		  unlink("/var/www/html/william/pdf_source/" . $Document . '.pdf');


Thank you so much Vailab for teh fast response!

If anyone can see some typos, tell me.

#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
This look good... I never really use this if else syntax, but I know it exist, so if it work, great

The only thing I could tell you, is that you should use relative path insted of absolute path, if ever you change the server, you won't have to go modify your script.

#5
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
Well this server is not mine, but from the company where... I don't know how englisch people say it, but google translate tells me it is internship :confused:
I am a student programmer getting experience at a real company, and this is my first application I am making for them. :rolleyes:

So how do you think I can make it better?

Thanks in advance!

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
If you are learning, please refer to every function you use that you do not already understand:

PHP: copy - Manual
PHP: rename - Manual

There are also various language versions, possibly the manual is in your language already.

My main concern is that you do not check copy's result, if it is false, such as if it could not copy the file: then you are unlinking the original file without a copy thus destroying data.

//what if the next line fails and returns false?
copy ("/var/www/html/william/pdf_source/" . $Document . '.pdf', "/var/www/html/william/pdf_result/" . $Document '.pdf');
//bye bye text and source: 
unlink("/var/www/html/william/pdf_txt/" . $Document . '.txt'); 
unlink("/var/www/html/william/pdf_source/" . $Document . '.pdf'

Also, I find your if statements very confusing (and indistinguishable without coffee), I would recommend if you use braces once, always use braces {} and not the silly old if: else: syntax.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
Well, it is impossible that the PDF file in "/var/www/html/william/pdf_source/" isn't there.

It goes like this:

- Scans come in a map (pdf_source)
- A pdf converter checks every 2 minutes if there is a pdf file, if so he convertes it to a .txt document and places that in (pdf_txt) while the orginal pdf file is still in the (pdf_source) map.

And my script gets the information from the txt, gives the pdf file a new name and location (pdf_result).
If my script can't find any information on the document he just relocates it and still have the same name, employees from the purchasing department have to rename the file name by them self.

And the txt file will always be deleted, this file is only there for a moment so the computer can find the information he needs to confirm the document.


And how can I program a if statement otherwise then? (I am kinda newby)

Thanks for teh reply again!

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You should acquire a basic understanding of PHP structure before you try to make anything of it

if(some_condition) {

} else {

}

Or nested:

if(some_condition) {
  if(some_other_condition) {
    ...
  }
} else {
  ...
}

Quote

Well, it is impossible that the PDF file in "/var/www/html/william/pdf_source/" isn't there.

If you are productively programming, and not testing directly on the server, then likely settings can be different or any number of possibilities can be played that will prevent the file from copying. If this ever were to happen, which it does, then previous file will be deleted and no copy will be made - even if the file is irrelevant this will break the application and be sloppy.

if(copy(...) == true) {
  //only then, should you unlink the other files.
}

I can think of numerous examples of people getting fired/ransacked for not doing simple things like these.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
Hey,

This is my new code:
It only removes the files if the copy is completed, if not both files will remain.
Even while the TXT file is useless, i can't delete it otherwise the PDF converter will convert the PDF file again, then we will create a loop.
The pdf converter doesn't convert the pdf file when the same file but then in txt is in the source map.

I also updated my layout :rolleyes:
Thanks for the help!


if ($cross_check => 1){

    if (file_exists("/var/www/html/william/pdf_source/" . $Document . '.pdf')) {

        rename("/var/www/html/william/pdf_source/" . $Document . '.pdf', "/var/www/html/william/pdf_result/" . $Po . '.pdf');

    else {

		copy ("/var/www/html/william/pdf_source/" . $Document . '.pdf', "/var/www/html/william/pdf_result/" . $Document '.pdf');

		}

	}

	if(copy ("/var/www/html/william/pdf_source/" . $Document . '.pdf', "/var/www/html/william/pdf_result/" . $Document '.pdf') == true {

		unlink("/var/www/html/william/pdf_source/" . $Document . '.pdf');

		unlink("/var/www/html/william/pdf_txt/" . $Document . '.txt');

	} 

}


William.

Wait a second, the "else" doesn't make any sense.
It says "if the file is there, rename it and put it in a aother folder".
and the else says actualy that "if the file isn't there, just put it in the other folder" wich it can't because the file isn't there. :lol:

---------- Post added at 09:08 AM ---------- Previous post was at 08:15 AM ----------

Okay, I just deleted the else.
So now it renames and relocates the file if it is there.
if the rename is true, then delete both files.
if the rename is not true then leave te files where they are.


if ($cross_check => 1){

    if (file_exists("/var/www/html/william/pdf_source/" . $Document . '.pdf')) { // kijken of file bestaat

        rename("/var/www/html/william/pdf_source/" . $Document . '.pdf', "/var/www/html/william/pdf_result/" . $Po . '.pdf'); // als het waar is: verplaats en nieuwe naam

	}

	if rename("/var/www/html/william/pdf_source/" . $Document . '.pdf', "/var/www/html/william/pdf_result/" . $Po . '.pdf') == true {

		unlink("/var/www/html/william/pdf_source/" . $Document . '.pdf');

		unlink("/var/www/html/william/pdf_txt/" . $Document . '.txt');

	} 

}


I think this is the way as it should be.

William

Edited by Dorgon, 12 September 2011 - 12:51 AM.
Changed my code a bit


#10
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
Hey guys,

I am working on the script again, it works great for a part.
But i am having some trouble with a while loop:


						if (file_exists("/var/www/html/pdf_check/pdf_result/" .$Po. '.pdf'))

							{

							$finished = false;

							$version = 0;

							while (! $finished )

								{

								$version ++;

								$newfile = "/var/www/html/pdf_check/result" .$Po. '_' .$version. '.pdf';

								if (! file_exists($newfile) )

									{

									rename("/var/www/html/pdf_check/pdf_source/" .$FILE. '.pdf', "/var/www/html/pdf_check/pdf_result/" .$Po.'_' .$version. '.pdf'); 

									unlink("/var/www/html/pdf_check/pdf_txt/" .$FILE. '.txt');

									$finished = true;

									}

								}

							}

						else

							{

							rename("/var/www/html/pdf_check/pdf_source/" . $FILE . '.pdf', "/var/www/html/pdf_check/pdf_result/" . $Po . '.pdf');

							unlink("/var/www/html/pdf_check/pdf_txt/" . $FILE. '.txt');	

							}

						}


The meaning is that if i get a file with the same $Po, that it renames the file to $Po_$version.
So the filenames get like this:
P167123.pdf
P167123_1.pdf
p167124.pdf
P167124_1.pdf
P167124_2.pdf
etc....
But when I run the script it only makes files up to _1.pdf
What is wrong with the while loop?

Thanks in advance!

#11
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
 $newfile = "/var/www/html/pdf_check/result" .$Po. '_' .$version. '.pdf'; 
You forget to add a "/" after result

#12
Dorgon

Dorgon

    Newbie

  • Members
  • PipPip
  • 24 posts
Aye,

Ive updated my script:


				$path_source = "/var/www/html/pdf_check/pdf_source/";

				$path_result = "/var/www/html/pdf_check/pdf_result/";

				$path_txt = "/var/www/html/pdf_check/pdf_txt/";

				

				if ($cross_check > 0)

					{ 

					if (file_exists ($path_source . $FILE. '.pdf')) 

						{ 

						if (file_exists($path_result . $Po . '.pdf'))

							{

							$exists = 0;

							$version = 0;

							$path = $path_result . $Po . '.pdf';

							

							while ( $exists < 1 )

								{

								$newpath = $path_result . $Po . '_' . $version . '.pdf';

								// echo $newpath;

							

								if (! file_exists ($newpath) )

									{

									rename($path_source . $FILE . '.pdf', $newpath); 

									unlink($path_txt . $FILE . '.txt');

									$exists++;

									}

								else

									{

									$version++;

									}

								}

							}

						}

						else

						{	

						rename($path_source . $FILE . '.pdf', $path_result . $Po . '.pdf'); 

						unlink($path_txt . $FILE . '.txt');

						}

					}

Still doesn't works 100% :/




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users