Moving files via php
Posted: Wed Apr 30, 2003 11:59 am
OK this is going to be fun. I have a number of files in one directory and I need to move them into a number of different directories, using information contained within a mySQL table.
Example.
My database contains two fields, 'pulse_urn' and 'pulse_abc'
A typical couple of records might have these vaules;
0001234567, ABC1234
0009864231, DEF8623
I have drop folder called PDF where files are written where name corresponds to the 'pulse_urn' value and the file extension, ie. ABC1234.pdf, DEF8623.pdf;
/Volumes/Pulse/Processed/PDF/ABC1234.pdf
/Volumes/Pulse/Processed/PDF/DEF8623.pdf
I want to pick these files up and put them into a new directory like this;
~/Sites/proofs/resources/pages/0001234567/ABC1234.pdf
~/Sites/proofs/resources/pages/0009864231/DEF8623.pdf
Ideally I want to know the way of creating a new directory named after the 'pulse_abc' value and move the file with the 'pulse_abc' value. I would not want to overwrite existing folders, but I do always want to overwrite existing files.
I have a php page (below) which currently simply connects to the database and selects the 'pulse_urn' and 'pulse_abc' values.
I also have a hidden value in a form with a value of '.pdf' with the intention of attaching this to the end of the 'pulse_urn' value to create a valid file name
<?php require_once('../../../Connections/proofs.php'); ?>
<?php
mysql_select_db($database_proofs, $proofs);
$query_pulse_import_files = "SELECT pulse_urn, pulse_abc FROM tbl_pulse";
$pulse_import_files = mysql_query($query_pulse_import_files, $proofs) or die(mysql_error());
$row_pulse_import_files = mysql_fetch_assoc($pulse_import_files);
$totalRows_pulse_import_files = mysql_num_rows($pulse_import_files);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php do { ?>
<form name="form1" method="post" action="">
<table width="100%" border="0">
<tr>
<td width="12%"><?php echo $row_pulse_import_files['pulse_abc']; ?></td>
<td width="20%"><?php echo $row_pulse_import_files['pulse_urn']; ?></td>
<td width="68%"> <label for="textfield"></label>
<input name="pulse_abc" type="hidden" id="pulse_abc" value="<?php echo $row_pulse_import_files['pulse_urn']; ?>">
<input name="pulse_urn" type="hidden" id="pulse_urn" value="<?php echo $row_pulse_import_files['pulse_abc']; ?>">
<input name="pulse_ext" type="hidden" id="pulse_ext2" value=".pdf">
</td>
</tr>
</table>
</form>
<?php } while ($row_pulse_import_files = mysql_fetch_assoc($pulse_import_files)); ?>
</body>
</html>
<?php
mysql_free_result($pulse_import_files);
?>
From here I really don't know what to do next, is there a php function I should be using or should I call a shell script passing values from the form?
I am developing this on Mac OS X using apache, php and mySQL so can run /bin/tcsh shell scripts if need be. Live rollout will be on RedHat linux so function wise should be very similar.
Any help, pointers or suggestions gratefully received.
Vu102
Example.
My database contains two fields, 'pulse_urn' and 'pulse_abc'
A typical couple of records might have these vaules;
0001234567, ABC1234
0009864231, DEF8623
I have drop folder called PDF where files are written where name corresponds to the 'pulse_urn' value and the file extension, ie. ABC1234.pdf, DEF8623.pdf;
/Volumes/Pulse/Processed/PDF/ABC1234.pdf
/Volumes/Pulse/Processed/PDF/DEF8623.pdf
I want to pick these files up and put them into a new directory like this;
~/Sites/proofs/resources/pages/0001234567/ABC1234.pdf
~/Sites/proofs/resources/pages/0009864231/DEF8623.pdf
Ideally I want to know the way of creating a new directory named after the 'pulse_abc' value and move the file with the 'pulse_abc' value. I would not want to overwrite existing folders, but I do always want to overwrite existing files.
I have a php page (below) which currently simply connects to the database and selects the 'pulse_urn' and 'pulse_abc' values.
I also have a hidden value in a form with a value of '.pdf' with the intention of attaching this to the end of the 'pulse_urn' value to create a valid file name
<?php require_once('../../../Connections/proofs.php'); ?>
<?php
mysql_select_db($database_proofs, $proofs);
$query_pulse_import_files = "SELECT pulse_urn, pulse_abc FROM tbl_pulse";
$pulse_import_files = mysql_query($query_pulse_import_files, $proofs) or die(mysql_error());
$row_pulse_import_files = mysql_fetch_assoc($pulse_import_files);
$totalRows_pulse_import_files = mysql_num_rows($pulse_import_files);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php do { ?>
<form name="form1" method="post" action="">
<table width="100%" border="0">
<tr>
<td width="12%"><?php echo $row_pulse_import_files['pulse_abc']; ?></td>
<td width="20%"><?php echo $row_pulse_import_files['pulse_urn']; ?></td>
<td width="68%"> <label for="textfield"></label>
<input name="pulse_abc" type="hidden" id="pulse_abc" value="<?php echo $row_pulse_import_files['pulse_urn']; ?>">
<input name="pulse_urn" type="hidden" id="pulse_urn" value="<?php echo $row_pulse_import_files['pulse_abc']; ?>">
<input name="pulse_ext" type="hidden" id="pulse_ext2" value=".pdf">
</td>
</tr>
</table>
</form>
<?php } while ($row_pulse_import_files = mysql_fetch_assoc($pulse_import_files)); ?>
</body>
</html>
<?php
mysql_free_result($pulse_import_files);
?>
From here I really don't know what to do next, is there a php function I should be using or should I call a shell script passing values from the form?
I am developing this on Mac OS X using apache, php and mySQL so can run /bin/tcsh shell scripts if need be. Live rollout will be on RedHat linux so function wise should be very similar.
Any help, pointers or suggestions gratefully received.
Vu102