basic file upload works
authorChristian Weiske <cweiske@cweiske.de>
Thu, 5 Apr 2012 16:23:48 +0000 (18:23 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 5 Apr 2012 16:23:48 +0000 (18:23 +0200)
data/templates/edit-file.htm
data/templates/edit.htm
data/templates/index.htm
src/phorkie/Repository/Post.php

index b320d8d36a5d5c0cd0cbf6747a12d18f146ac735..53c2c014f4b6dc9e427a9b07faae5adae7d7bb59 100644 (file)
   <textarea name="files[{{fileid}}][content]" id="content_{{fileid}}" cols="80" rows="15" class="content">{{file.getContent}}</textarea>
   <div class="row-fluid">
    <div class="span9">
-    &nbsp;
-    <!--
-    <label for="upload_{{fileid}}" class="inline">Replace with file upload:</label>
+    <label for="upload_{{fileid}}" class="inline">Replace with upload:</label>
     <input type="file" name="files[{{fileid}}][upload]" id="upload_{{fileid}}" class="btn"/>
-    -->
    </div>
    {% if not newfile %}
    <div class="span3" style="text-align: right">
index 19494b65609d8d0f3dfe4a627f41366ac73cddb5..f1f4a51f05011373526654baa0af8c12450e1e8d 100644 (file)
@@ -2,7 +2,7 @@
 {% block title %}Edit paste{% endblock %}
 
 {% block content %}
-<form method="post" action="{{repo.getLink('edit')}}">
+<form method="post" action="{{repo.getLink('edit')}}" enctype="multipart/form-data">
  <div class="control-group well pastedata">
    <label for="description">Description</label>
    <input type="text" name="description" id="description" value="{{repo.getDescription}}"/>
index 31913bcf3285bacda85395addf5777638aa2c850..05c4c05a3bd68040dd2c370b9c0c350d1028b7c2 100644 (file)
@@ -2,7 +2,7 @@
 {% block title %}New paste{% endblock %}
 
 {% block content %}
-<form method="post" action="/">
+<form method="post" action="/" enctype="multipart/form-data">
  <div class="control-group well pastedata">
    <label for="description">Description</label>
    <input type="text" name="description" id="description" value="{{description}}"/>
index b9d9be5aa626e6af73ca4556e2debc6481f66196..8cd9323ca1625f082f2dd2e55eb464a22596b4d2 100644 (file)
@@ -29,8 +29,12 @@ class Repository_Post
         $this->repo->setDescription($postData['description']);
 
         $bChanged = false;
-        foreach ($postData['files'] as $arFile) {
-            if ($arFile['content'] == '' && $arFile['name'] == '') {
+        foreach ($postData['files'] as $num => $arFile) {
+            $bUpload = false;
+            if ($_FILES['files']['error'][$num]['upload'] == 0) {
+                //valid file upload
+                $bUpload = true;
+            } else if ($arFile['content'] == '' && $arFile['name'] == '') {
                 //empty (new) file
                 continue;
             }
@@ -39,8 +43,12 @@ class Repository_Post
             $name        = $this->sanitizeFilename($arFile['name']);
 
             if ($name == '') {
-                $name = $this->getNextNumberedFile('phork')
-                    . '.' . $arFile['type'];
+                if ($bUpload) {
+                    $name = $this->sanitizeFilename($_FILES['files']['name'][$num]['upload']);
+                } else {
+                    $name = $this->getNextNumberedFile('phork')
+                        . '.' . $arFile['type'];
+                }
             }
 
             $bNew = false;
@@ -73,6 +81,14 @@ class Repository_Post
                     ->addArgument($file->getFilename())
                     ->execute();
                 $bChanged = true;
+            } else if ($bUpload) {
+                move_uploaded_file(
+                    $_FILES['files']['tmp_name'][$num]['upload'], $file->getPath()
+                );
+                $command = $vc->getCommand('add')
+                    ->addArgument($file->getFilename())
+                    ->execute();
+                $bChanged = true;
             } else if ($bNew || $file->getContent() != $arFile['content']) {
                 file_put_contents($file->getPath(), $arFile['content']);
                 $command = $vc->getCommand('add')