|
Posted by petersprc on 11/30/06 05:00
Yeah that's a great point. The function should fall-through after an
error, but they are E_USER_ERRORs...
dyer85 wrote:
> That was quite a helpful post. I just had a suggestion for your error
> handling in the addFileToDir function. You don't specify the error type
> for the trigger_error function. Its default is E_USER_NOTICE, which
> allows execution to continue. You should change it to E_USER_ERROR, so
> execution stops. If one error occurs, in this case, there's no point in
> continuing.
>
> Curtis
>
> On Nov 25, 6:35 pm, "petersprc" <peters...@gmail.com> wrote:
> > Hi,
> >
> > Some links you might use:
> >
> > phpThumb image resizing library:
> > http://phpthumb.sourceforge.net
> >
> > Image manipulation code samples:
> > http://www.zend.com/codex.php?CID=344
> >
> > Some mysql insert examples with PDO:
> > http://wiki.cc/php/PDO_Basics#Insert_with_bound_parameters
> >
> > Here's a function which can be used to add an uploaded file to your
> > image directory:
> >
> > <?
> >
> > // addFileToDir: Move a file to a directory and rename
> > // the file if necessary to avoid clobbering any existing
> > // file with the same name.
> >
> > function addFileToDir($path, $dir, $useRename = true)
> > {
> > $ok = false;
> > $baseName = basename($path);
> > $tries = 4;
> >
> > for ($i = 0; $i < $tries; $i++) {
> > if ($i == 0) {
> > $newPath = $dir . '/' . $baseName;
> > } else {
> > if (!isset($rootName)) {
> > preg_match('/^(.*?)(\.[^.]*)?$/', $baseName, $matches);
> > $rootName = $matches[1];
> > $dotExt = count($matches) > 2 ? $matches[2] : '';
> > }
> > $newPath = "$dir/$rootName-" . uniqid(mt_rand(), true) .
> > $dotExt;
> > }
> >
> > if (file_exists($newPath)) {
> > if ($i == $tries - 1) {
> > trigger_error("Failed to get unique destination path " .
> > "while adding file \"$path\" to directory \"$dir\".");
> > }
> > $file = false;
> > } elseif ($i < $tries - 1) {
> > $file = @fopen($newPath, 'x');
> > } elseif (!($file = fopen($newPath, 'x'))) {
> > trigger_error("Failed to open destination path \"$newPath\" " .
> > "while moving file \"$path\".");
> > }
> >
> > if ($file) {
> > if (!fclose($file)) {
> > trigger_error("Failed to close file \"$newPath\".");
> > } else {
> > if ($useRename) {
> > if (!rename($path, $newPath)) {
> > trigger_error("Failed to move file \"$path\" to " .
> > "\"$newPath\".");
> > } else {
> > $ok = true;
> > }
> > } elseif (!copy($path, $newPath)) {
> > trigger_error("Failed to copy file \"$path\" to " .
> > "\"$newPath\".");
> > } elseif (!unlink($path)) {
> > trigger_error("Failed to delete file \"$path\".");
> > } else {
> > $ok = true;
> > }
> > }
> >
> > if (!$ok && !unlink($newPath)) {
> > trigger_error("Failed to delete file \"$newPath\".");
> > }
> >
> > break;
> > }
> > }
> >
> > return $ok ? $newPath : false;
> >
> > }function testIt()
> > {
> > $path = dirname(__FILE__) . '/test.jpg';
> > $dir = dirname(__FILE__) . '/images';
> > $newPath = addFileToDir($path, $dir);
> > if ($newPath === false) {
> > echo "Failed to add file.";
> > } else {
> > echo "File was moved to \"$newPath\".";
> > }
> >
> > }error_reporting(E_ALL);
> > testIt();
> >
> > ?>
> >
> > gsoguerri...@gmail.com wrote:
> > > Hi,
> > > I have limited knowledge in php and I am having trouble with uploading
> > > an image to a remote directory and resizing it if it's larger and
> > > renaming it to a unique id, while at the same time I would like to
> > > create a record in mysql database. I've tried to find some tutorials
> > > but I've had trouble finding any good ones. Wondering if anybody has
> > > good examples or link to a good tutorial.
> >
> > > Thanks in advance.
Navigation:
[Reply to this message]
|